我正在尝试create_python_script函数,该函数在当前工作目录中创建一个新的python脚本,向其中添加由'comments'变量声明的注释行,并返回新文件的大小。我得到的输出是0,但应该是31。不确定我在做什么错。
import os
def create_python_script(filename):
comments = "# Start of a new Python program"
with open("program.py", "w") as file:
filesize = os.path.getsize("/home/program.py")
return(filesize)
print(create_python_script("program.py"))
import os
def create_python_script(filename):
comments = "# Start of a new Python program"
with open(filename, "w") as f:
f.write(comments)
filesize = os.path.getsize(filename)
return(filesize)
print(create_python_script("program.py"))
请注意,输入参数以前未使用,现在已更改。