Read and Write on File


#1

I write this code:

def save(name):
    nameFile = name+’.txt’
    f = open(nameFile,‘w’)
    f.write(name)
    f.close()
    
    f = open(nameFile,‘r’)
    msg = f.read()
    f.close()
    print(msg)

in python this code works, but on Zerynth I have a problem.
I debug the code and it stops before the instruction " f.write(name)". Why? How can I solve this problem ? Should I import some libraries?
thank you for all.


#2

Hi virginiodv,
you should take a look at Zerynth Filesystem example https://docs.zerynth.com/latest/official/core.zerynth.stdlib/examples/examples.html#filesystem :wink:
The open function is currently implemented under the os module https://docs.zerynth.com/latest/official/core.zerynth.stdlib/docs/official_core.zerynth.stdlib_os.html
which needs a mounted filesystem to correctly work, so you may need an SD card to write your files to:
https://docs.zerynth.com/latest/official/core.zerynth.stdlib/docs/official_core.zerynth.stdlib_fatfs.html  
Let me know if this helps :slight_smile: