File mode 'w+'

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
cagiva
Posts: 22
Joined: Wed Dec 14, 2016 4:49 pm

File mode 'w+'

Post by cagiva » Mon Oct 23, 2017 10:33 pm

Does MicroPython support the 'w+' file mode?
w+ : Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.

Code: Select all

  with open('log.tx', 'w+') as f:
         v = int(f.read())
         v = v+1
         f.write(str(v))

User avatar
benalb
Posts: 22
Joined: Fri May 19, 2017 1:23 pm

Re: File mode 'w+'

Post by benalb » Tue Oct 24, 2017 6:51 am

cagiva wrote:
Mon Oct 23, 2017 10:33 pm
Does MicroPython support the 'w+' file mode?

Code: Select all

MicroPython v1.9.2-144-g98dd126 on 2017-10-05; ESP module with ESP8266
Type "help()" for more information.
>>> with open('test.txt', 'w+') as f:
...     f.write('hello, world')
...     
...     
... 
12
>>> import uos
>>> uos.listdir("/")
['boot.py', 'webrepl_cfg.py', 'max7219.py', 'lanmon.py', 'test.txt']

Code: Select all

 benalb@t410  ~/tmp/micropython  
ampy -p /dev/ttyUSB0 get test.txt 
hello, world
on esp8266, yes. :)

Post Reply