Python 2.7: Print to File

后端 未结 6 957
情深已故
情深已故 2020-11-29 00:16

Why does trying to print directly to a file instead of sys.stdout produce the following syntax error:

Python 2.7.2+ (default, Oct  4 2011, 20:06         


        
6条回答
  •  鱼传尺愫
    2020-11-29 00:39

    print is a keyword in python 2.X. You should use the following:

    f1=open('./testfile', 'w+')
    f1.write('This is a test')
    f1.close()
    

提交回复
热议问题