Python write to a file returns empty file

后端 未结 2 1007
长发绾君心
长发绾君心 2020-11-28 15:12

I am trying to do simple commands to write hello world to a file:

50 complexity:test% python2.7
Python 2.7.3 (default, Feb 11 2013, 12:48:32)
[GCC 4.4.6 2012         


        
2条回答
  •  情深已故
    2020-11-28 15:44

    You need to close the file:

    >>> f.close()
    

    Also, I would recommend using the with keyword with opening files:

    with open("/export/home/vignesh/resres.txt","w") as f:
        f.write("hello world") 
        f.write("\t".join(["hello","world"]))
    

    It will automatically close them for you.

提交回复
热议问题