-1

I searched and found the below code which was written in python to find and delete the empty folders in the drive. when i try to delete the folders, it is giving the error message below. Can i have the modified code or idea to search and delete corrupt folders and files in the drive.

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)
        

Error Message:

 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
1
  • How are you running your phyton script? python my_script.py? Commented Jul 17, 2021 at 11:38

1 Answer 1

0

OSError: [Errno 5] Input/output error

In case you are running your code from a terminal, this error is likely to occur when you do not have an active stdout. To avoid issues with stdout, you may redirect the output from the terminal to a specified location as suggested here:

python my_script.py > /dev/null &
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.