Traverse all subordinate files and folders in the directory, including subfolders, to find empty files and empty folders and delete them
def Clean_empty(path):
"""
Iterate through all subfolders and subfiles under a file, cleaning up empty folders and files
path:file path
"""
for (dirpath,dirnames,filenames) in os.walk(path):
for filename in filenames:
file_folder=dirpath+'/'+filename
# print(file_folder)
if os.path.isdir(file_folder):
if not os.listdir(file_folder):
print(file_folder)
# os.rmdir(dirpath+filename)
elif os.path.isfile(file_folder):
if os.path.getsize(file_folder) == 0:
print(file_folder)
os.remove(file_folder)
print(path, 'clean over!')
if __name__ == "__main__":
path = '/data/git/ocr-platform/data/annotation_data/recognize/dataset/ocr_dataset_etc'
Clean_empty(path)
Done!
Read More:
- Python recursively traverses all files in the directory to find the specified file
- How to Solve Python Pandas Read or Import Files Error
- Python Redis: How to batch fuzzy Delete Keys
- Pytorch: How to Handle error warning conda.gateways.disk.delete:unlink_or_rename_to_trash(140)
- Python FileNotFoundError: [Errno 2] No such file or directory: ‘objects/epsilon.pkl
- [Solved] Python project runs the open() function error: FileNotFoundError: [Errno 2] No such file or directory
- Python environment error, bad interpreter: there is no file or directory
- The solution of no such file or directory and cannot load native module running error of python3 pyinstaller after packaging
- Python: How to get the size of the picture (byte/kb/mb)
- Python PIP Fatal error in launcher: Unable to create process using ‘“e:\program files\programdata
- Python: How to Reshape the data in Pandas DataFrame
- How to Fix keyerror in Python dictionary lookup
- How to Solve Python WARNING: Ignoring invalid distribution -ip (e:\python\python_dowmload\lib\site-packages)
- An introduction to sys modules in Python and how packages are imported and used
- [Solved] python Error: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.
- Python traverses all files under the specified path and retrieves them according to the time interval
- Python: How to Solve multiprocessing module Error in Windows
- [Solved] error: (-215:Assertion failed) !_src.empty() in function ‘cv::cvtColor‘
- How to Fix “HTTP error 403: forbidden” in Python 3. X