如何使用python查找和删除损坏的文件

2024-10-02 02:31:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我搜索并找到了下面用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

Tags: pathruninsrc文件夹forifos
1条回答
网友
1楼 · 发布于 2024-10-02 02:31:16

OSError: [Errno 5] Input/output error

如果您是从终端运行代码,当您没有活动的stdout时,可能会发生此错误。为了避免stdout出现问题,您可以将终端的输出重定向到建议的指定位置here

python my_script.py > /dev/null &

相关问题 更多 >

    热门问题