I have tried a simple write .txt file in a python script using Raspberry Pi B+ but is not working. It does not show any error but I can see the file. The code is:
file = open('log.txt','r')
file.write('hi'+'\n')
file.close()
I have tried with 'w+' , 'a' , 'w'; and reboot.
python Desktop/BQ/log.py
And when opening the file it shows the error that does not exist in the directory:
cat Desktop/BQ/log.txt
Thanks for any help.
'r'is for reading only. You probably want'w'or possibly'a'.Desktopfolder? Is it on the Raspberry Pi or on your local machine? If it's on the Pi, then are you runningcaton that same machine? If you're running all these things on your desktop machine, then what does this question have to do with Raspberry Pi?'log.txt', then that file name is interpreted as being relative to the current working directory, not relative to the location of the.pyfile. So if you're launching the program aspython Desktop/BQ/log.pyand it writes tolog.txt, the file won't be atDesktop/BQ/log.txt, it will be atlog.txt(presumably~/log.txtifDesktopis inside your home directory).