Jul-09-2022, 12:08 PM
Hello everybody,
I have a folder with many text-files, some of them are emtpy but have mutliple lines (for example, someone edited an empty text file, pressed enter and saved the file). I wanted to create a script where it checks every file in a specified path and deletes all those files. I've manged to delete really empty files but haven't gotten to the point, where those multiple-blank-line files have been deleted. This is my code so far:
I have a folder with many text-files, some of them are emtpy but have mutliple lines (for example, someone edited an empty text file, pressed enter and saved the file). I wanted to create a script where it checks every file in a specified path and deletes all those files. I've manged to delete really empty files but haven't gotten to the point, where those multiple-blank-line files have been deleted. This is my code so far:
#!/usr/bin/env python3 #Imports import sys import os #Folder configuration def remove_empty(path): print(list(os.walk(path))) for (dirpath, folder_names, files) in os.walk(path): for filename in files: file_location = dirpath + '/' + filename if os.path.isfile(file_location): if os.path.getsize(file_location) == 0: os.remove(file_location) #Deletion if __name__ == "__main__": path = 'C:\Path\to\folder\' remove_empty(path) #End sys.exit()