473,396 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Wildcard Strings in Path

4
Okay, this is what I need to do, and I keep getting confused.
I need to remove all files in a folder, then delete the folder, it's part of an installer I'm making, and it creates a directory, I just don't know how to delete it. This is what I have right now:
curDir = os.getcwd()
os.remove(os.path.join(curDir, *)
So it will remove all the files in the directory it is in. How do I use the wildcard to make it all files in the directory? And then how do I delete the directory?
Also, how would I copy the script to another location?

Any help would be much appreciated!
Nov 11 '09 #1
7 7877
bvdet
2,851 Expert Mod 2GB
To delete all the files in the current working directory, create a file list with:
Expand|Select|Wrap|Line Numbers
  1. os.listdir(os.getcwd())
Then you can iterate on the list. Delete a directory with:
Expand|Select|Wrap|Line Numbers
  1. os.rmdir(path)
Nov 11 '09 #2
Apros
4
If I do the first, I end up with:

Traceback (most recent call last):
File "<pyshell#19>", line 1, in <module>
os.remove(os.listdir(os.getcwd()))
TypeError: remove() argument 1 must be string, not list

So how do I make all of the list, which can vary thus needing of a wildcard, into strings? I'm sorry, but I just picked up programming a little while ago, and Python just a couple of weeks ago, so I'm not really sure how it all works.

If I try the second one, it gives me an error saying it is not able to, because it is in use by another program. How do I make it not do that? And I want to make it so the srcipt deletes everything in the folder, including itself... SOrry if I'm coming off as someone who doesn't know anything. Ireally don't, just need a small more push.
Nov 11 '09 #3
bvdet
2,851 Expert Mod 2GB
When I say iterate on the list:
Expand|Select|Wrap|Line Numbers
  1. for fn in fileList:
  2.     os.remove(os.path.join(os.getcwd(), fn)
Are you trying to remove the current working directory? If so, change directories, then try to delete it.
Expand|Select|Wrap|Line Numbers
  1. >>> os.chdir('..')
  2. >>> os.rmdir('directory_name')
  3. >>> 
Nov 11 '09 #4
Apros
4
Grr...
Okay, this is the script I have so far...

Expand|Select|Wrap|Line Numbers
  1. import os
  2. curDir = os.listdir(os.getcwd())
  3. for fn in fileList: 
  4.     os.remove(os.path.join(curDir, fn)
I'm going to guess that I'm doing it completely wrong...
So... the listdir is supposed to list the files that are in the current directory
Then for the filename in the list, remove ((Current DIrectory)+Filename)
Correct?
See, I do that, and it gives me this error:

Traceback (most recent call last):
File "<pyshell#18>", line 2, in <module>
os.remove(os.path.join(curDir, fn))
TypeError: remove() argument 1 must be string, not list

So it appears that I'm making a list of the files, how do I change each object in the list to a string?
I'm thinking something of doing the list[:]... and that's all I got.
Again, apologize for the lack of programming knowledge.
I googled for hours, and couldn't find enough info, except for the RE module, and all the tut's I found were confusing. And it appears that I don't need it.
Nov 12 '09 #5
bvdet
2,851 Expert Mod 2GB
Uh, try this:
Expand|Select|Wrap|Line Numbers
  1. import os
  2. fileList = os.listdir(os.getcwd())
  3. for fn in fileList: 
  4.     os.remove(os.path.join(os.getcwd(), fn))
You were close. Be careful with this code. You could accidentally delete the wrong files.
Nov 12 '09 #6
Apros
4
:D
Thank you, so much!
It worked!
*bows to the epic superiority of bvdet*

Now, if you want to delete the folder, it would be

path = os.getcwd()
os.rmdir(path)

correct, no?
Nov 12 '09 #7
bvdet
2,851 Expert Mod 2GB
@Apros
You will need to change to another directory. If you attempt to delete the current working directory, you will receive an error that may look like this:

OSError: [Errno 13] Permission denied: 'directory name whatever'
Nov 12 '09 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: greg.kujawa | last post by:
I am passing along an Index Server 2.0 query though URL strings calling the ASP. I am searching for records by a person's last name and their phone number. Something like: ...
3
by: Adam | last post by:
Its my understanding that in asp.net 2.0 you can write an httpmodule that will acts as a wildcard handler and pass requests on to the proper engine, for instance, asp or perl for example, In the...
7
by: SlimFlem | last post by:
I have searched hard for 2 days on this and keep hitting a wall. I have a custom IHttpHandler setup to do Url mappings to prop up some old Urls for our site. I have also created a wildcard...
3
by: george.lengel | last post by:
Hello experts, I have been struggling for days to solve this problem and every suggestion I find via Google does not work for me. There is probably a solution out there that will do what I want,...
78
by: wkehowski | last post by:
The python code below generates a cartesian product subject to any logical combination of wildcard exclusions. For example, suppose I want to generate a cartesian product S^n, n>=3, of that...
2
by: Jan Kucera | last post by:
Hi, I have virtual MyFolder/* and MyFolder/MySubFolder/* mapped to an httphandler in the web.config file and all works perfectly on the asp.net development server. However on the IIS6/Win2003 I'm...
4
by: questionit | last post by:
Is there any wildcard available to compare 2 strings E.g: California Califarnia and return True if both the strings are very similar like above ? i know "*" does check only after the end...
5
by: Krustov | last post by:
I have the following list of image files . When searching the latest (numbered file) in this particular case its background_4.*** and its a .jpg file - but - the latest file in the list could...
2
by: Arch Stanton | last post by:
I have an aspx page with a text box. My user enters text to search for and presses a button; the text is passed via a QueryString to another aspx page and used in a SQL search. The wildcard...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.