0

It's easy to find people asking how to do this, but there is never an answer that applies to me from what I can tell. I simply want to create permissions, in generality, to write to new files in a subdirectory. Within python I call

filename = 'dfs_err_results/err_results_st'+str(station_id)
file = open(filename,'w')
pickle.dump(results, file)
file.close()

but it fails at open() with IOError: [Errno 13] Permission denied: 'dfs_err_results/err_results_st5'. If I enter with sudo python, that error doesn't occur, but it's annoying to have to remember that and to have to check progress within sudo ps x where there are a ton of processes.

Am I correct there's no way to alter the subdirectory for future files using chmod? Or no way to have lines of code within the python script that set the permissions?

Thanks!

user
  • 621
  • 1
  • 9
  • 21
  • There was a similar topic, take a look if it helps http://stackoverflow.com/questions/5191878/change-to-sudo-user-within-a-python-script – xmp Mar 29 '15 at 22:22
  • I'm sorry can you expand on that? I'm trying to use this `import subprocess` / `subprocess.call(["/usr/bin/sudo", "/usr/bin/id"])` function but I don't think I'm understanding it. This is sending it to execute shell commands under sudo right? And then return to my python function as a normal user? But how do I do this file opening in python within that command? Do I need to make another whole file that I'm sending arguments to? – user Mar 29 '15 at 22:38
  • 1
    This isn't really a Python question but rather a Linux question. You should ask on ServerFault or SuperUser. Of course, if there was a way to get elevated privileges without authentication, that would be a big flaw. You should first design your access rights, then there are a lot of solutions to implement what you need. The `os` module in python provides functions to set permissions on files (but of course only the ones your can access). – Cilyan Mar 29 '15 at 22:55
  • I think it's a Python question, too, given that the answer lies within the subprocess module. – user Mar 30 '15 at 06:09

3 Answers3

3

Yes it does execute command with sudo. For exmaple, before openning the file you can change its write permission. Do whatever you need and change permission back. I don't have linux by hand at this moment, by try something like this:

subprocess.call(['/usr/bin/sudo', 'chmod', '777', filename])
with open(filename) as file:
    pickle.dump(results, file)
xmp
  • 104
  • 1
  • 4
  • I think this only works if the file already exists, but I want it to be able to create a new one the way file.open(name,'w') usually does. And having it use a text editor like vim to do so breaks the python script. Is there a way to create a file without adding any content? – user Mar 30 '15 at 05:59
0

This is mostly contributed by xmp but it does require one more line to create the file:

subprocess.call(['/usr/bin/sudo', 'touch', filename])
subprocess.call(['/usr/bin/sudo', 'chmod', '777', filename])
file = open(filename,'w')
pickle.dump(results, file)
file.close()

Thanks for the help!

user
  • 621
  • 1
  • 9
  • 21
  • Now if only I could suppress the output '0's that come along with it in Python 2.7. [These](http://stackoverflow.com/questions/699325/suppress-output-in-python-calls-to-executables) suggestions aren't compatible and browsing other Popen suggestions hasn't worked so far. – user Mar 30 '15 at 06:20
0

That's what worked for me:

os.chmod(filename, stat.S_IRWXO | stat.S_IRWXG | stat.S_IRWXU)
with open(filename, 'w') as f:
     pickle.dump(i, f)

this gives full permissions for everything you can choose the permissions you want by changing the stat mode: http://www.tutorialspoint.com/python/os_chmod.htm

Ella Cohen
  • 1,375
  • 1
  • 10
  • 14