Thomas K. answered 06/21/19
Effective and understanding tutor in West Los Angeles
You need to open your file in w+ mode.
Thomas K. answered 06/21/19
Effective and understanding tutor in West Los Angeles
You need to open your file in w+ mode.
Cheryl F. answered 05/04/19
Scientific Programming is Science aided by Software
In python2, the flag for opening a file in mode 'rw' is 'r+'
$ python2
Python 2.7.15 (default, May 1 2018, 16:44:37)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> file = open ('file_for_rw.txt','r+')
>>> file.write ('hiya\n')
>>> file.close()
>>> ^D
$ cat file_for_rw.txt
hiya
In python3, the flag for opening a file is 'x' -- because who needs consistency?
$ python3
Python 3.6.3 |Anaconda custom (64-bit)| (default, Oct 6 2017, 12:04:38)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> file = open ('file_for_x.txt','x')
>>> file.write('r+ for python2, x for python3')
29
>>> file.close()
>>>
$ cat file_for_x.txt
r+ for python2, x for python3
Get a free answer to a quick problem.
Most questions answered within 4 hours.
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.