Tuesday 15 January 2013

Missing file when using Python 2.7.3 File.write() function -


If you want to type a file to write below the Python script:

  #! / Usr / bin / python count = 1 fo = open ('DbCount.txt', 'w') fo.write (str (count)) # fo.flush () fo.close ()   

The Dbcount.txt file that was placed in the same folder as the script (attempt to modify Dbcount.txt). I do not see any changes in the txt file and no error interpreter, its very strange, does not show any help? Firstly, always use with the version of the version, which will always close the file on errors.

  #! Open / usr / bin / python count = 1 ('dbCount.txt', 'w') as fo.write (str (count))   

then 'W' every time your file overwrites you write it if you want to add, then use 'a'. .

Regarding your specific problem, have you seen only in the directory of your script, or are you calling in the current directory from the script? As you have written your code, you are related to the file path that you write, from which you execute your code.

Try:

  Import with OS count = 1 Open (OS .path.join (os.path.dirname (__ file__), 'dbcctant txt'), For 'W') such as: fo.write (str (counts))   

Then the output should be in the same path as its script, in the same path as DbCount.txt .

No comments:

Post a Comment