A collection of sloppy snippets for scientific computing and data visualization in Python.
import os dirPath = "/home/giu/pyExperiments/toRemove" fileList = os.listdir(dirPath) for fileName in fileList: os.remove(dirPath+"/"+fileName)
dude, perfect ! thanks for this it really actually works, unlike all the crap over at stackoverflow.
Hey,Appreciated.A little improved codeimport osdirPath = "/home/giu/pyExperiments/toRemove"fileList = os.listdir(dirPath)[ os.remove(os.path.abspath(os.path.join(dirPath,fileName))) for fileName in fileList ]
Hey buddy ! ,Your fileList do not works, as it work as a string not as a directory.Instead use this:[os.remove(os.path.abspath(os.path.join(destination, fileName))) for fileName in os.listdir(destination)
Readability in OP is better.
You will get an error if there is a folder in your dirPath. The 'crap' from stackoverflow does actually work:for filename in fileList: item=os.path.join(dirPath,fileList) if os.path.isfile(item): os.remove(item)
this creates an error, the correct code is:for filename in fileList: item=os.path.join(dirPath,filename) if os.path.isfile(item): os.remove(item)
gud 1..it helped..:)
Oops... using this code removed ALL dirs now I don;t have the OSWhat can do?
One small tip:instead of dirPath+"/"+fileNameI would recommendos.path.join(dirPath, fileName), which will insert a forward slash for Unix and Linux users, and a backslash for the Windows guys.An alternative just popped into my mind:dirPath = 'some/path'shutil.rmtree(dirPath)os.mkdir(dirPath)
what a wonderful piece of code.. big thanks dude!!
Thank you! Worked perfect for me.
Thanks works perfectly
Well, that was a great help when I was urgent.Thank You
Thanks! works perfectly
Note: Only a member of this blog may post a comment.
dude, perfect ! thanks for this it really actually works, unlike all the crap over at stackoverflow.
ReplyDeleteHey,
ReplyDeleteAppreciated.
A little improved code
import os
dirPath = "/home/giu/pyExperiments/toRemove"
fileList = os.listdir(dirPath)
[ os.remove(os.path.abspath(os.path.join(dirPath,fileName))) for fileName in fileList ]
Hey buddy ! ,
DeleteYour fileList do not works, as it work as a string not as a directory.
Instead use this:
[os.remove(os.path.abspath(os.path.join(destination, fileName))) for fileName in os.listdir(destination)
Readability in OP is better.
ReplyDeleteYou will get an error if there is a folder in your dirPath. The 'crap' from stackoverflow does actually work:
ReplyDeletefor filename in fileList:
item=os.path.join(dirPath,fileList)
if os.path.isfile(item):
os.remove(item)
this creates an error, the correct code is:
Deletefor filename in fileList:
item=os.path.join(dirPath,filename)
if os.path.isfile(item):
os.remove(item)
gud 1..it helped..:)
ReplyDeleteOops... using this code removed ALL dirs now I don;t have the OS
ReplyDeleteWhat can do?
One small tip:
ReplyDeleteinstead of
dirPath+"/"+fileName
I would recommend
os.path.join(dirPath, fileName), which will insert a forward slash for Unix and Linux users, and a backslash for the Windows guys.
An alternative just popped into my mind:
dirPath = 'some/path'
shutil.rmtree(dirPath)
os.mkdir(dirPath)
what a wonderful piece of code.. big thanks dude!!
ReplyDeleteThank you! Worked perfect for me.
ReplyDeleteThanks works perfectly
ReplyDeleteWell, that was a great help when I was urgent.
ReplyDeleteThank You
Thanks! works perfectly
ReplyDelete