1#It will creates Text file in path directory using name of file.txt
2import os
3
4path = 'some/path/to/file.txt'
5if not os.path.exists(path):
6 with open(path, 'w') as f:
7 f.write('Hello, world')
8#==== OR ===
9mode = 'a' if os.path.exists(path) else 'w'
10with open(path, mode) as f:
11 f.write('Hello, world!\n')
1#Updated dec 2020
2#It will creates Text file in path directory using name of file.txt
3import os
4
5path = 'some/path/to/file.txt'
6if not os.path.exists(path):
7 with open(path, 'w') as f:
8 f.write('Hello, world')
9#==== OR ===
10mode = 'a' if os.path.exists(path) else 'w'
11with open(path, mode) as f:
12 f.write('Hello, world!\n')
13