Tested in Windows 10, 10.0.17134.556.
import os.path textFile = "c:\\Users\\Shared\\TextFile.txt" doesFileExist=os.path.isfile(textFile) if doesFileExist == True: file = open(textFile,"w+")#w+ w, a+, r file.write('Write to textfile 1\n') file.write('Write to textfile 2\n') file.write('Write to textfile 3\n') file.close()
Notes
Mode | Description |
---|---|
‘r’ | This is the default mode. It Opens file for reading. |
‘w’ | This Mode Opens file for writing. If file does not exist, it creates a new file. If file exists it truncates the file. |
‘x’ | Creates a new file. If file already exists, the operation fails. |
‘a’ | Open file in append mode. If file does not exist, it creates a new file. |
‘t’ | This is the default mode. It opens in text mode. |
‘b’ | This opens in binary mode. |
‘+’ | This will open a file for reading and writing (updating) |