我搜索并找到了下面用python编写的代码,用于查找和删除驱动器中的空文件夹。 当我试图删除文件夹时,它会给出下面的错误消息。 我可以修改代码或想法来搜索和删除驱动器中损坏的文件夹和文件吗
import os
for root, dirs, files in os.walk('//run//media//halovivek//testing'):
for file in files:
src_file = os.path.join(root, file)
if os.path.getsize(src_file) == 0:
os.remove(src_file)
dir_list = []
for root, dirs, files in os.walk('//run//media//halovivek//testing'):
dir_list.append(root)
for root in dir_list[::-1]:
if not os.listdir(root):
os.rmdir(root)
错误消息:
Traceback (most recent call last):
File "/home/halovivek/PycharmProjects/pythonProject/deleteEmpty.py", line 5, in <module>
if os.path.getsize(src_file) == 0:
File "/usr/lib/python3.9/genericpath.py", line 50, in getsize
return os.stat(filename).st_size
OSError: [Errno 5] Input/output error: '//run//media//halovivek//testing/.Trash-1000/files/Songs Folder/My Music/SAD = UNNA NENA.mp3'
Process finished with exit code 1
如果您是从终端运行代码,当您没有活动的
stdout
时,可能会发生此错误。为了避免stdout出现问题,您可以将终端的输出重定向到建议的指定位置here:相关问题 更多 >
编程相关推荐