Python Forum
Building a script to check size of file upon creation - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Building a script to check size of file upon creation (/thread-25576.html)



Building a script to check size of file upon creation - mightyn00b - Apr-03-2020

Greetings, I built a script that checks the size of a file upon creation, but the value is definitely inaccurate (the file is not empty and thus should not be 0 bytes).

Here is what happens:

import os

def create_python_script(filename):
    comments = "# Start of a new Python Program"
    #filesize = 0
    with open(filename, 'w') as new_file:
        new_file.write(comments)
        abspath = os.path.join(os.getcwd(),filename)
        filesize = os.path.getsize(abspath)
        
    return(filesize)

print(create_python_script('newprogram.py'))
a zero is returned however it should be returning roughly 31 bytes:

lamidotijjo Week2 $ ls -l
total 24
-rw-rw-r-- 1 lamidotijjo lamidotijjo 31 Apr 3 12:54 newprogram.py

As one can see it should read 31 bytes.


RE: Building a script to check size of file upon creation - mightyn00b - Apr-03-2020

I solved it, thanks!


RE: Building a script to check size of file upon creation - Larz60+ - Apr-04-2020

please share your solution s others can benefit.