Python Forum
Building a script to check size of file upon creation
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Building a script to check size of file upon creation
#1
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.
Reply
#2
I solved it, thanks!
Reply
#3
please share your solution s others can benefit.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Converted EXE file size is too large Rajasekaran 0 1,445 Mar-30-2023, 11:50 AM
Last Post: Rajasekaran
  please check this i wanna use a csv file as a graph xCj11 5 1,437 Aug-25-2022, 08:19 PM
Last Post: deanhystad
  check if a file exist on the internet and get the size kucingkembar 6 1,714 Apr-16-2022, 05:09 PM
Last Post: kucingkembar
  Code to check folder and sub folders for new file and alert fioranosnake 2 1,871 Jan-06-2022, 05:03 PM
Last Post: deanhystad
  How to acquire an imported file creation time thunderspeed 4 1,885 Sep-23-2021, 04:27 PM
Last Post: Larz60+
  Check last time file was accessed Pavel_47 4 2,759 Jun-01-2021, 05:47 PM
Last Post: Yoriz
  How to check if a file has finished being written leocsmith 2 7,689 Apr-14-2021, 04:21 PM
Last Post: perfringo
  xml file creation from an XML file template and data from an excel file naji_python 1 2,069 Dec-21-2020, 03:24 PM
Last Post: Gribouillis
  Check if a file exists. Pedroski55 5 3,251 Sep-08-2020, 10:01 AM
Last Post: Pedroski55
  Web Form to Python Script to Text File to zip file to web wfsteadman 1 2,098 Aug-09-2020, 02:12 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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