在windows平台下用Python做工程的时候,经遇到需要对某类文件生成路径的情况,此时常用的方法是在cmd命令行中去dir。但是这样会生成一个文件。例如列出所有C盘中jpg图像的路径,在cmd中输入dir C:\*.jpg /s/b > list.txt,或者在python程序中os.system(dir C:
def removeDir(dirPath):
if not os.path.isdir(dirPath):
return
files = os.listdir(dirPath)
try:
for file in files:
filePath=os.path.join(dirPath,file)
if os.path.isfile(filePath):
os.remove(filePath)
elif os.path.isdir(filePath):
removeDir(filePath)
os.rmdir(dirPath)
except Exception,e:
print e