查找文件夹下的所有子文件
import os allfile=[] def dirlist(path): filelist=os.listdir(path) #fpath=os.getcwd() for filename in filelist: # allfile.append(fpath+‘/‘+filename) filepath=os.path.join(path,filename) if os.path.isdir(filepath): dirlist(filepath) else: allfile.append(filepath) #print filepath dirlist(‘C:/Users/Lin/Desktop/python/TEST/a‘) print allfile
删除文件夹下的所有子文件
import os def removefile(filedir): for name in os.listdir(filedir): filepath=os.path.join(filedir,name) if os.path.isfile(filepath): os.remove(filepath) if __name__=="__main__": removefile(‘C:/Users/Lin/Desktop/python/TEST/a‘)
import os import re def removefile(filedir): for name in os.listdir(filedir): filepath=os.path.join(filedir,name) if os.path.isfile(filepath): s=r‘\d*\.jpg‘ reg=re.compile(s) match=reg.search(name) if match: os.remove(filepath) if __name__=="__main__": removefile(‘C:/Users/Lin/Desktop/python/TEST‘) # print k
时间: 2024-10-25 08:26:48