小型python txt文件编写程序的无效语法错误

时间:2019-07-18 23:15:08

标签: python

我正在尝试创建/写入txt文件,但是,我从https://www.guru99.com/reading-and-writing-files-in-python.html获得的代码未呈现。

def main():

f = open("JTF.txt", "w+")

for i in range(10)
    f.write("This is line %d\r\n" % (i+1))

f.close()

if __name__=="__main__":
    main()

# The error i am receiving when I run the above code is as follows:
#     def main()
#             ^
# SyntaxError: invalid syntax

1 个答案:

答案 0 :(得分:0)

Python使用空格块语法。另外,在for循环之后,您需要添加:

def main():
    f = open("JTF.txt", "w+")

    for i in range(10):
        f.write("This is line %d\r\n" % (i+1))

    f.close()

if __name__=="__main__":
    main()