By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,483 Members | 3,261 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,483 IT Pros & Developers. It's quick & easy.

Wildcard Strings in Path

P: 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
Share this Question
Share on Google+
7 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
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

P: 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
Expert Mod 2.5K+
P: 2,851
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

P: 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
Expert Mod 2.5K+
P: 2,851
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

P: 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
Expert Mod 2.5K+
P: 2,851
@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

Post your reply

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