0
  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. but when i put this code in python program it shows no such file and directory. but file mytxt gets created into the / folder

  • Try it without /. As it requires special permissions. – Ganesh Jun 17 '17 at 04:59
  • Two solutions 1. Remove / 2. run script using superuser permissions (sudo). 1st is recommended. – Arvind Jun 17 '17 at 05:04
  • Kindly show ur Python code.. u must use Python module `import os` for executing linux commands via Python like `os.system('ls')` . – AB Abhi Jun 17 '17 at 05:05
  • @Abhishakegupta she is using commands module https://docs.python.org/2/library/commands.html , which does the same but depreacated. – Arvind Jun 17 '17 at 05:07
  • There has been no activity for some time. Can you please accept an answer? – RaphaMex Dec 01 '17 at 18:25

2 Answers2

0

Here is your answer:

Python Print String To Text File

If you write python code and not a shell script, I guess you want to use python built-in functions to manage your files.

RaphaMex
  • 2,652
  • 11
  • 28
0

I just has a similar issue on a server with php code. I edited the files as root, but then the web server could not write to the file.

The issue is that Apache (or your web server) does not have read/write access. 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.

Phillip Williams
  • 416
  • 2
  • 10