简体   繁体   English

无法将数据插入使用Python代码中的touch创建的空文件中

[英]not able to insert data into an empty file created using touch in Python code

  commands.getoutput("touch /mytxt.txt")
  x="hello"
  commands.getoutput("echo x | cat >> /mytxt.txt".format(x))

I tried to run this code in python live interpreter , it runs perfectly ,data is inserted into the file. 我试图在python live解释器中运行此代码,它可以完美运行,数据已插入文件中。 but when i put this code in python program it shows no such file and directory. 但是当我将此代码放入python程序时,它没有显示此类文件和目录。 but file mytxt gets created into the / folder 但是文件mytxt被创建到/文件夹中

Here is your answer: 这是您的答案:

Python Print String To Text File Python打印字符串到文本文件

If you write python code and not a shell script, I guess you want to use python built-in functions to manage your files. 如果您编写的是python代码而不是Shell脚本,那么我想您想使用python内置函数来管理文件。

I just has a similar issue on a server with php code. 我只是在具有php代码的服务器上有一个类似的问题。 I edited the files as root, but then the web server could not write to the file. 我以根用户身份编辑了文件,但是Web服务器无法写入该文件。

The issue is that Apache (or your web server) does not have read/write access. 问题是Apache(或您的Web服务器)没有读/写访问权限。 like as a Group owner to the file/folder 例如作为文件/文件夹的组所有者

Setting the GROUP permission, and set the folder so the web server has full read/write access too. 设置GROUP权限,并设置文件夹,以便Web服务器也具有完全的读/写访问权限。

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

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