| | |
Removing directories
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
Firstly, you can use glob to get a list containing the name of each directory that starts with td like such:
Next, you'll want to iterate over the list that glob returns by doing something along the lines of
Finally, to remove there exists a built-in
dirs = glob.glob(td.*) You can find the documentation about glob here.Next, you'll want to iterate over the list that glob returns by doing something along the lines of
for dir in dirs: .Finally, to remove there exists a built-in
os.rmdir(dir) which will remove the directory specified. Read up on the various operating system interfaces that the os module offers here. Last edited by jlm699; Jul 23rd, 2009 at 2:47 pm. Reason: icode tag fix
•
•
Join Date: Nov 2008
Posts: 4
Reputation:
Solved Threads: 1
shutil.rmtree will remove non-empty directories.
shutil.rmtree(path[, ignore_errors[, onerror]])ΒΆ
Delete an entire directory tree; path must point to a directory (but not a symbolic link to a directory). If ignore_errors is true, errors resulting from failed removals will be ignored; if false or omitted, such errors are handled by calling a handler specified by onerror or, if that is omitted, they raise an exception.
If onerror is provided, it must be a callable that accepts three parameters: function, path, and excinfo. The first parameter, function, is the function which raised the exception; it will be os.path.islink(), os.listdir(), os.remove() or os.rmdir(). The second parameter, path, will be the path name passed to function. The third parameter, excinfo, will be the exception information return by sys.exc_info(). Exceptions raised by onerror will not be caught.
shutil.rmtree(path[, ignore_errors[, onerror]])ΒΆ
Delete an entire directory tree; path must point to a directory (but not a symbolic link to a directory). If ignore_errors is true, errors resulting from failed removals will be ignored; if false or omitted, such errors are handled by calling a handler specified by onerror or, if that is omitted, they raise an exception.
If onerror is provided, it must be a callable that accepts three parameters: function, path, and excinfo. The first parameter, function, is the function which raised the exception; it will be os.path.islink(), os.listdir(), os.remove() or os.rmdir(). The second parameter, path, will be the path name passed to function. The third parameter, excinfo, will be the exception information return by sys.exc_info(). Exceptions raised by onerror will not be caught.
•
•
Join Date: May 2009
Posts: 70
Reputation:
Solved Threads: 2
If you are using bash in the linux terminal, it is simple to just remove all directories containing "td" with:
rm -r *td*
First you may want to use ls *td* to make sure there are no other directories that would be removed in this process by mistake.
I know this is a python question, but if you didn't know how to do it in bash, it is quite simple.
rm -r *td*
First you may want to use ls *td* to make sure there are no other directories that would be removed in this process by mistake.
I know this is a python question, but if you didn't know how to do it in bash, it is quite simple.
•
•
Join Date: Jul 2009
Posts: 8
Reputation:
Solved Threads: 0
•
•
•
•
Firstly, you can use glob to get a list containing the name of each directory that starts with td like such:dirs = glob.glob(td.*)You can find the documentation about glob here.
Next, you'll want to iterate over the list that glob returns by doing something along the lines offor dir in dirs:.
Finally, to remove there exists a built-inos.rmdir(dir)which will remove the directory specified. Read up on the various operating system interfaces that the os module offers here.
Well when i use this code it gives me an error and tells me that one of the directories is not empty....in fact none of them are empty, i just want to get rid of them no matter what.
![]() |
Similar Threads
- empty linux trash (Python)
- Removing an item from head of linked list (C)
- General Tips for Mac OS X (Mac tips 'n' tweaks)
- Ubuntu Directories Exercise (Getting Started and Choosing a Distro)
- Removing W2k SP4 from Dual boot with XP SP2 (Windows NT / 2000 / XP)
- XP: Lots of directories; Can't delete (Windows NT / 2000 / XP)
- Explorer windows disapear when accessing some directories and web pages (Viruses, Spyware and other Nasties)
- Completely Removing Spyware (Viruses, Spyware and other Nasties)
- Removing Bridge.dll (Web Browsers)
Other Threads in the Python Forum
- Previous Thread: command line boolean
- Next Thread: Search string help
| Thread Tools | Search this Thread |
alarm assignment avogadro beginner bluetooth character cmd code customdialog cx-freeze data decimals dictionary directory dynamic error examples excel exe file float font format function generator gnu graphics gui halp homework http ideas import input itunes java leftmouse line linux list lists logging loop maintain maze module mouse number numbers output parsing path port prime programming projects push py2exe pygame pyglet pyqt python queue random recursion schedule screensaverloopinactive script scrolledtext slicenotation sqlite ssh stdout string strings sudokusolver table terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode urllib urllib2 variable ventrilo verify vigenere web webservice wikipedia windows wxpython xlib






