0

I'm trying to check the file size of a bunch of files from a specific directory, but not being able, could you guys help me?

import os
dir_path = 'C:\\temp\\cmm_temp\\tmp\\'
files = os.listdir(dir_path)
with open(dir_path+files, "r") as arq:
    for arq in files:
        file_size = os.stat(str(arq)).st_size
        if file_size == 0:
            print("The file  is empty: "+str(arq) + str(file_size))
        else:
            print("The file is not empty: " +str(arq) + str(file_size))

that's the output

C:\USER_ATU\PycharmProjects\CMM_SDL157\venv\Scripts\python.exe C:/USER_ATU/PycharmProjects/CMM_SDL157/venv/Scripts/testFile.py
Traceback (most recent call last):
  File "C:/USER_ATU/PycharmProjects/CMM_SDL157/venv/Scripts/testFile.py", line 35, in <module>
    with open(dir_path+files, "r") as arq:
FileNotFoundError: [Errno 2] No such file or directory: "C:\\temp\\cmm_temp\\tmp\\['MST_DatFileList.txt', 'MST_TtsFileParsed.txt', 'test_fileZero.txt', 'ZTN_DatFileList.txt', 'ZTN_TtsFileParsed.txt']"

Process finished with exit code 1

Thanks

1

3 Answers 3

1

files is a list. You need to iterate over it and pass single file to os.stat. I simplified your code a bit:

import os
dir_path = 'C:\\temp\\cmm_temp\\tmp\\'
for fname in os.listdir(dir_path):
    full_path = os.path.abspath(os.path.join(dir_path, fname))
    file_size = os.stat(full_path).st_size
    print(f"The file{' not ' if file_size else ' '}is empty: {full_path}, {file_size}")

Note, os.listdir will yield both files and folders and thus the code will print size of both files and folders.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for you reply, it worked, but I trying to add an exception to my for, when I got 2 files empty it breaks and return the exception, something like this: dir_path = 'C:\\temp\\cmm_temp\\tmp\\' totFilesEmpty = 0 for fname in os.listdir(dir_path): try: full_path = os.path.abspath(os.path.join(dir_path, fname)) file_size = os.stat(full_path).st_size if file_size == 0: totFilesEmpty = totFilesEmpty + 1 except Exception as ab: print("number of empty files == 02", ab) but, it's not going to the Exception, any ideas why?
just to be clear, I need to check if the files from a folder are empty, if I have 2 empty files, I want to break my code and return an Exception, sorry, but I'm new to python, so I might be missing something
0

Try this

import os
b = os.path.getsize("__path__")
print(b) 

Comments

0

You could simply check if the file is a regular file and then use os.path.getsize(𝙛𝙞𝙡𝙚)
Example:

import os

files = os.listdir(𝙙𝙞𝙧_𝙥𝙖𝙩𝙝)
for file in files:
    if os.path.isfile(file):
        print(file, os.path.getsize(file))

2 Comments

Hi Matteo, thanks, but no output was returned : Process finished with exit code 0
have you replaced 𝙙𝙞𝙧_𝙥𝙖𝙩𝙝 with the path of your folder? remember that this command only returns the size of regular files and not directories.

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.