Python Forum
Delete empty text files [SOLVED]
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Delete empty text files [SOLVED]
#3
Use pathlib.Path for better abstraction.


This could not work, because the quote is escaped by the last \.
path = 'C:\Path\to\folder\'
Better:
path = r'C:\Path\to\folder'
raw-string, so escape don't work, only the quote '/" could be escaped.

sys.exit is not required at the end, because if there is only the Main-Thread and there are no further statements and no loop, the program ends.

To know if a file has only white spaces inside, you have to read the whole file. This is a big problem, if there are big files. So you need to have a limit for this. Then you could read the files, which are small enough, stripping them and when the result is an empty str/bytes, then delete the file.

The annotations are not mandatory, but they give the programmer an Idea which types should be used.
There is no control, if the right types are used. This is for example if you use an IDE and you want to give the
IDE better hints. So, you'll use this in future at some point.
Code:
#!/usr/bin/env python3
from __future__ import annotations

from pathlib import Path

ONE_MIB = 1048576 

def remove_empty(root: str | Path, max_size: int = ONE_MIB) -> None:
    """
    Removes all files, which are 0 bytes big
    Removes all files, which are smaller or equal than max_size and have only whitespace inside.
    This prevents striping files which are too big (takes time to read into memory and stripping)
    """
    root = Path(root)
    for path in root.rglob("*"):
        if not path.is_file():
            # we want only real files
            # path could also be a directory
            continue

        size = path.stat().st_size
        # if size == 0, unlink
        # or if size is smaller as max_size, then read the whole content of file into
        # memory, stripping whitespace. Empty bytes/str is False

        if size == 0 or size <= max_size and not path.read_bytes().strip():
            path.unlink()


if __name__ == "__main__":
    path = r"C:\Path\to\folder"
    # path had a backslash at the end, which excapes the qoute.
    # This is also for raw-strings True
    remove_empty(path)
I like it to use double quotes, because I can see them better.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
Delete empty text files [SOLVED] - by AlphaInc - Jul-09-2022, 12:08 PM
RE: Delete empty text files - by Gribouillis - Jul-09-2022, 01:26 PM
RE: Delete empty text files - by DeaD_EyE - Jul-09-2022, 01:36 PM
RE: Delete empty text files - by AlphaInc - Jul-09-2022, 02:10 PM
RE: Delete empty text files - by Gribouillis - Jul-09-2022, 01:38 PM
RE: Delete empty text files - by DeaD_EyE - Jul-09-2022, 02:15 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] Loop through directories and files one level down? Winfried 3 365 Apr-28-2024, 02:31 PM
Last Post: Gribouillis
Question [solved] compressing files with python. SpongeB0B 1 711 May-26-2023, 03:33 PM
Last Post: SpongeB0B
  Help replacing word in Mutiple files. (SOLVED) mm309d 0 902 Mar-21-2023, 03:43 AM
Last Post: mm309d
  [SOLVED] [sqilte3] Check if column not empty? Winfried 5 1,217 Jan-28-2023, 12:53 PM
Last Post: Winfried
  azure TTS from text files to mp3s mutantGOD 2 1,809 Jan-17-2023, 03:20 AM
Last Post: mutantGOD
  delete all files and subdirectory from a main folder mg24 7 1,735 Oct-28-2022, 07:55 AM
Last Post: ibreeden
  delete all files which contains word sql_Table1 mg24 2 923 Sep-15-2022, 10:05 PM
Last Post: mg24
  [SOLVED] [BeautifulSoup] How to get this text? Winfried 6 2,087 Aug-17-2022, 03:58 PM
Last Post: Winfried
  Writing into 2 text files from the same function paul18fr 4 1,770 Jul-28-2022, 04:34 AM
Last Post: ndc85430
  select files such as text file RolanRoll 2 1,257 Jun-25-2022, 08:07 PM
Last Post: RolanRoll

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020