-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
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
vivek rajagopalan
  • 843
  • 3
  • 13
  • 22

1 Answers1

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 &
Swaroop
  • 1,219
  • 3
  • 16
  • 32