39.4k views
2 votes
The create_python_script function creates a new python script in the current working directory, adds the line of comments to it declared by the 'comments' variable, and returns the size of the new file. Fill in the gaps to create a script called "program.py".

def create_python_script(filename):
comments = "# Start of a new Python program"
with ___:
filesize = ___
return(filesize)
print(create_python_script("program.py"))

User Clete
by
4.3k points

1 Answer

6 votes

Answer:

Following are the code to this question:

with open(filename,"w+") as file: #use an open method that open file in w+ mode

file.write(comments)#use write method that inputs the value in the file

file.close()#closing the file

import os#import package os for file

filesize = os.path.getsize(filename)#defining a variable filesize that uses the getsize method to calculate its size.

Step-by-step explanation:

Following are the full program to this question:

def create_python_script(filename):#defining method create_python_script that accept filename variable as the parameter

comments = "# Start of a new Python program"#defining a string varaible comments that holds string value

with open(filename,"w+") as file: #use an open method that open file in w+ mode

file.write(comments)#use write method that inputs the value in the file

file.close()#closing the file

import os#import package os for file

filesize = os.path.getsize(filename)#defining a variable filesize that uses the getsize method to calculate its size.

return filesize#return filesize value

print(create_python_script("program.py"))#use print method that call create_python_script and print its return value

Output:

please find program file and its output.

Program description:

In the above-given program, a method "create_python_script" is declared that accept the "filename" as the parameter, inside the method a comments string variable is declared, that holds string value and define an open method to open "program.py" file and assign the string value in the file.

In the next line, the "getsize" method is defined, which counts string value in digits and returns its value and use the print method to call the "create_python_script" with parameter a print its value.

The create_python_script function creates a new python script in the current working-example-1
User Aeupinhere
by
5.1k points