aeko sato
asked on
Delete Folder Contents and multiple files in Python
hello
How can I delete the contents of a local folder and multiple files in Python?
The current project is for Linux.
i would like delete the extention *.pyc and files name sommefilestmp.flv and files on folder log
i use os with os.remove just this line delete 1 files and i want to delete multiple files in one line
ty
How can I delete the contents of a local folder and multiple files in Python?
The current project is for Linux.
i would like delete the extention *.pyc and files name sommefilestmp.flv and files on folder log
i use os with os.remove just this line delete 1 files and i want to delete multiple files in one line
ty
why multiple files with one line, why not write a function which does this (and which you optionally call recursively for sub dirs)?
Are you processing a single directory or a tree (dir and sub-dirs)?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You can feed the output of glob() or iglob() into os.remove()
Example:
Example:
os.remove(glob.glob('c:\users\aikimark\downloads\Python\*.pyc'))
@aikimark:
my python version does not allow to pass a list to os.remove(), and glob.glob() returns a list. perhaps python 3 allows this?
if you really wanted to write a oneliner you could do:
my python version does not allow to pass a list to os.remove(), and glob.glob() returns a list. perhaps python 3 allows this?
d:\work>python
Python 2.7.4 (default, Apr 6 2013, 19:54:46) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os, glob, sys
>>> glob.glob('*.xxx')
['f1.xxx', 'f2.xxx', 'f3.xxx']
>>> os.remove(glob.glob('*.xxx'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: must be string, not list
if you really wanted to write a oneliner you could do:
map(os.remove, glob.glob('*.xxx'))
ASKER
ty it works :)
that is why I included iglob(), since it returns an iterable
@aikimark
iglob doesn't work either:
python 3.2.3 doesn't seem to handle this either.
Perhaps this is a feature just working with very
iglob doesn't work either:
$ python -c "import os; import glob; os.remove(glob.iglob('*.xxx'))"
Traceback (most recent call last):
File "<string>", line 1, in <module>
TypeError: coercing to Unicode: need string or buffer, generator found
python 3.2.3 doesn't seem to handle this either.
$ python3
Python 3.2.3 (default, Feb 27 2014, 21:31:18)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os; import glob; os.remove(glob.iglob('*.xxx'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Can't convert 'generator' object to str implicitly
>>> import os; import glob; os.remove(glob.glob('*.xxx'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Can't convert 'list' object to str implicitly
Perhaps this is a feature just working with very
well, dang. :-(
Did you try a comprehension syntax?
[os.remove(x) for x in glob.glob("*.xxx")]
@aikimark
your most recent suggestion works of course as you pass just one value per call to os.remove()
On the other hand it's not shorter than my previously suggested
Your and my one-linerare a little weird because the create an intermediate list of None values just to have it destroyed afterwards.
Both are only one line shorter than the explicit for loop of my initial answer.
I'm not sure it's really worth a list comprehension or a map() call
your most recent suggestion works of course as you pass just one value per call to os.remove()
On the other hand it's not shorter than my previously suggested
map(os.remove, glob.glob('*.xxx'))
Your and my one-linerare a little weird because the create an intermediate list of None values just to have it destroyed afterwards.
Both are only one line shorter than the explicit for loop of my initial answer.
for filename in filenames:
os.remove(filename)
I'm not sure it's really worth a list comprehension or a map() call
@gelonida
I agree with your analysis. I only posted my post-closure comments to help future readers and to confirm that my earlier comment wasn't too 'crazy'. I'm still learning Python, so it still takes me a bit longer than it should to arrive at an answer. Thanks for your participation.
I agree with your analysis. I only posted my post-closure comments to help future readers and to confirm that my earlier comment wasn't too 'crazy'. I'm still learning Python, so it still takes me a bit longer than it should to arrive at an answer. Thanks for your participation.