简体   繁体   English

python 将字符串写入文件给出空文件

[英]python write string to file gives empty file

I try to write a string to a file in Python and fail,我尝试将字符串写入 Python 中的文件并失败,

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.但是,文件“test.json”不存在。 if I for example remove an "a" in "saaalam" it works.例如,如果我删除“saaalam”中的“a”,它就可以工作。

Your code is working.您的代码正在运行。 You should check if python process has the right to create that file in your OS.您应该检查 python 进程是否有权在您的操作系统中创建该文件。 In Linux and MacOS you can provide yourself access to write in the current directory with:在 Linux 和 MacOS 中,您可以为自己提供在当前目录中写入的权限:

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.您应该检查 IDE 中的工作目录或尝试从控制台运行脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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