2

Consider:

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)
        cwd = os.getcwd()
        fpath = os.path.abspath(filename)
        filesize = os.path.getsize(fpath)
    return(filesize)

print(create_python_script('newprogram.py'))

I am getting the result as zero, but it should get "31".

2
  • 3
    Text files have a default buffer in the order of 4kiB. Since your text is smaller, it is not flushed until the file is closed. Check the file size outside of the with context. Commented Apr 11, 2020 at 9:31
  • 1
    Does this answer your question? Python write to a file returns empty file Commented Apr 11, 2020 at 9:33

4 Answers 4

3

You didn't close your file before trying to get its size, as you do it inside the with block. Take it outside:

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)
        cwd=os.getcwd()
        fpath = os.path.abspath(filename)
        print(fpath)

    filesize=os.path.getsize(fpath)
    return(filesize)

print(create_python_script('newprogram.py'))
# 31
Sign up to request clarification or add additional context in comments.

Comments

0

First opening the file with write permissions to add the text in the file. Then opening the file with read permissions to get the size of the file.

import os

def create_python_script(filename):
    comments = "# Start of a new Python program"
    with open(filename, 'w') as pd:
        pd.write(comments)

    with  open(filename, "r"):
        filesize = os.path.getsize(filename)
        print(filesize)
    return filesize

print(create_python_script("program.py"))

2 Comments

Hello and welcome to stackoverflow. You help is appreciated, but please provide explanations for answer & code
Welcome to Stack Overflow! While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply. From Review
-1
import os

def create_python_script(filename):
  comments = "# Start of a new Python program"
  with open(filename, 'w') as file:
    file.write(comments)
    file.close()
    filepath = os.path.abspath(filename)
    filesize = os.path.getsize(filepath)
  return(filesize)

print(create_python_script("program.py"))

#this will give you correct result

1 Comment

Welcome to StackOverflow. While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.
-1

This one is working perfectly too!

    def create_python_script(filename):
      import os
      comments = "# Start of a new Python program"
      with open(filename,'w')as file:
         file.write(comments)
      filesize = os.path.getsize(filename)
      return(filesize)
    print(create_python_script("program.py"))

Comments

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.