简体   繁体   中英

python write string to file gives empty file

I try to write a string to a file in Python and fail,

mystr = '{"a": "{\'a\': [\'b\': [\'\\saaaalam\\Mira.h\']}, {\'c\': [\'C:\\a\\t.exe\']}", "b": "open"}'

with open('test.json', 'w') as out_file:
   out_file.write(mystr)

The file "test.json" does not exist, However. if I for example remove an "a" in "saaalam" it works.

Your code is working. You should check if python process has the right to create that file in your OS. In Linux and MacOS you can provide yourself access to write in the current directory with:

chmod 744 .

Another option is you are looking for your file at the wrong place. You should check the working directory in your IDE or try to run the script from the console.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM