正在try 删除给定目录中除一个文件之外的所有文件.我查看了其他相关帖子,但我的问题似乎特定于从os.listdir获取名称,以及需要带有扩展名的完整路径才能使用os.Remove:
# delete files from the save path but preserve the zip file
if os.path.isfile(zip_path):
for clean_up in os.listdir(data_path):
if not clean_up.endswith(tStamp+'.zip'):
os.remove(clean_up)
出现此错误:
Line 5: os.remove(clean_up)
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'firstFileInListName'
我认为这是因为os.listdir没有捕获每个文件的文件扩展名(打印的os.listdir(Data_Path),并且只获得了没有扩展名的文件的名称)
除了以tStamp+‘.zip’结尾的文件外,我可以做些什么来删除data_path中的所有文件?