Computing.Net > Forums > Programming > Help on deleting files in a dir

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Help on deleting files in a dir

Reply to Message Icon

Name: mustang509
Date: October 9, 2008 at 12:02:22 Pacific
OS: vista
CPU/Ram: 1250
Product: pavilion
Comment:

First off I would like to mention I am using python. I already have a batch file that can do it. I want to know how to do it in python as well. I have done alot of reading and cant come up with the answer. Well I should say without 20 lines of code. Maybe thats how it has to be done but thought I would ask here first. What I have tried is using a wildcard but I keep getting an error.

import os
os.remove("c:/users/xxxx/desktop/new file/*.*")

Tried different variations for the wildcard but nothing seems to work. Any help would be greatly appriciated. Also would like to thank ghostdog for all his help in the past. If I cant wildcard all the files in the "new file" folder what about deleting the whole folder?



Sponsored Link
Ads by Google

Response Number 1
Name: klint
Date: October 10, 2008 at 03:29:05 Pacific
Reply:

remove just removes a single file. Try the command help(os). It gives you help on all the functions in that module. You may find listdir or removedirs useful.


0

Response Number 2
Name: mustang509
Date: October 10, 2008 at 12:33:26 Pacific
Reply:

Thanks klint it seems windows or at least vista wont let you delete a directory if it has files in it. I think I found what I was looking for. I will post it here maybe it will help someone else. The only trick to get it to work is you have to convert it to a exe with py2exe or you get a windows error in vista. The only thing the exe file does is allow me to run the file with admin privileges. I dont know any way around this if any one could shed some light on it I would appreciate it. Other whise it works perfect. Heres what I found.

import os
for each in os.listdir("c:/Program Files/xxxx/yyyy"): # reads all the files in y

    os.remove("c:/Program Files/xxxx/yyyy/"+each) # deletes all the files in y 
os.rmdir("c:/Program Files/xxxx/yyyy") # deletes y

Well thats the shortest I could find. Any other ideas would be appreciated.


0

Response Number 3
Name: klint
Date: October 10, 2008 at 14:36:35 Pacific
Reply:

Glad you got it working. I have not used Vista but have read many people's complaints about difficulties using it due to protection. Perhaps you can try running python.exe with admin privileges?


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Help on deleting files in a dir

Delete Files in a directory older than X days www.computing.net/answers/programming/delete-files-in-a-directory-older-than-x-days/19781.html

Batch file to delete files in a dir www.computing.net/answers/programming/batch-file-to-delete-files-in-a-dir/14599.html

Run script on each file in directory www.computing.net/answers/programming/run-script-on-each-file-in-directory/19818.html