Link to home
Start Free TrialLog in
Avatar of aeko sato
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
Avatar of MerijnB
MerijnB
Flag of Netherlands image

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
Avatar of gelonida
gelonida
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can feed the output of glob() or iglob() into os.remove()
Example:
os.remove(glob.glob('c:\users\aikimark\downloads\Python\*.pyc'))

Open in new window

@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?

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

Open in new window



if you really wanted to write a oneliner you could do:
map(os.remove, glob.glob('*.xxx'))

Open in new window

Avatar of aeko sato
aeko sato

ASKER

ty it works :)
that is why I included iglob(), since it returns an iterable
@aikimark

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

Open in new window


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

Open in new window


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")]

Open in new window

@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
map(os.remove, glob.glob('*.xxx'))

Open in new window


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)

Open in new window


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.