What permission should set to the directory to avoid Permission Denied? - python

I'm using python manage.py runserver in development mode and getting
IOError at /cmanager/upload/save
[Errno 13] Permission denied: u'/tmp/temp/IMG_27022014_183050.png'
Once I run the chmod -R 775 "/tmp/temp/", it works. But on every shutdown/restart of computer, that directory from /tmp gets deleted automatically, Since need to create it manually.
settings.py
CONTENT_STORAGE_PATH /tmp/temp/
controller
if not os.path.exists(settings.CONTENT_STORAGE_PATH):
try:
os.makedirs(settings.CONTENT_STORAGE_PATH, 0644)
except OSError, e:
self.raiseException(e)
content_storage_path = os.path.join(settings.\
CONTENT_STORAGE_PATH, f.name)
with open(content_storage_path, 'wb+') as destination:
for chunk in f.chunks():
destination.write(chunk)
How to avoid this Permission Denied error.
Is it good to set permissions? like: os.chmod(content_storage_path, 0600). If so what it should be? 0775?
Note: I'm going to change the location "/tmp/temp/" to "/var/www/temp/" in production mode while configuring with Apache/NginX

It is not good to set permissions with os.chmod inside your script, because you can not escalate priveleges any higher than whatever the process itself has.
I don't think you should be using os module at all here. Use builtin tempfile module for a tried-and-tested cross-platform method of doing what you need.
http://docs.python.org/2/library/tempfile.html
If your permission issues remain, you need to resolve them outside the script - they are environment issues and not the responsibility of the code.

Related

Permissions Error and No such file or Directory

if not os.path.exists('/var/log/'):
os.makedirs('/var/log/')
print(log_filepath)
os.chmod(log_filepath, stat.S_IWOTH)
f_log_in = open(log_filepath, "a")
Without the chmod command, it will throw an error saying permission denied for the f_log_in file open command.
f_log_in = open(log_filepath, "a")
PermissionError: [Errno 13] Permission denied: '/var/log/s3_sync.log'
When I include the os.chmod command, it says:
os.chmod(log_filepath, stat.S_IWOTH)
FileNotFoundError: [Errno 2] No such file or directory: '/var/log/s3_sync.log'
Are there any other ways of approaching this?
EDIT: THIS IS NOT A DUPLICATE, I DELETED THE OTHER ONE.
The file indicated in log_filepath doesn't exist. Thus, you cannot simply open it, as you do with open. See this answer for more info, but you need w+ or a+ to also create the file.
The second error you get is because of exactly the message - the file doesn't exist, so you can't change the permissions to write to it.
Now, you might still have a problem if you are not executing the program as a user with sufficient permissions to access /var/log (or wherever log_filepath points to). You have to run as a user with sufficient permission - there is no way around that, either by running as a user with that permission or by changing the permissions of the directory itself so that the user you are executing as would have sufficient permission.

Permission denied on shutil.move on image files

I am trying to move some files around. I can move any extension type except .png, .jpg, or .gif. When I try to move those types of files I get "IOError: [Errno 13] Permission denied" even though I am the admin. Code below
import os, glob, shutil
dir = r'C:\\Users\\jcan4\\Desktop\\testmove\\*'
print(dir)
files = glob.glob(dir)
files.sort(key=os.path.getmtime)
for i, file in enumerate(files, start=1):
print(file)
oldext = os.path.splitext(file)[1]
shutil.move(file, 'Attachment-%s' % (i) + oldext)
First things first, you're double escaping your dir variable:
print(r'C:\\Users\\jcan4\\Desktop\\testmove\\*')
# Yields 'C:\\\\Users\\\\jcan4\\\\Desktop\\\\testmove\\\\*' !!
# What you really meant was either one of the following:
dir_harderToRead = 'C:\\Users\\jcan4\\Desktop\\testmove\\*'
dir_easyToRead = r'C:\Users\jcan4\Desktop\testmove\*'
If you are still experiencing the error, it's because you are not giving the python script permissions to move the file. There are a couple ways to get around this:
Windows
(This applies to the asked question)
Open command prompt (I see your file path and am assuming you're on windows) with administrative rights. (see here)
Change ownership of the images to you. (see here for windows 10 or here for windows 7)
Linux (MacOS)
(This applies to people on Linux that may have the same problem)
Run the python script with root privileges:
# At command line
sudo python your_script_name.py
Change ownership of file to yourself:
# At command line
# Changes ownership of entire directory (CAREFUL):
chmod 755 /absolute/path/to/dir
chmod 755 relative/path/to/dir
# Or you can change file by file:
chmod 755 /absolute/path/to/file
chmod 755 relative/path/to/file
For more info, I used this site on permissions. (If someone has a numerical value than 755 for chmod please say so.)

PermissionError on writable file in Jenkins

I'm trying to open a file in python that was opening fine before with this syntax, but now that I've rearranged directories I am getting a PermissionError and I cannot pinpoint why. I have directory ahs_reports containing files create_ahs_report.py and message_body.html. ahs_report is contained in a directory called reports. message_body.html has permissions -rwxrwxrwx so it should be writable. ahs_report/ also has the same permissions. However, when I run create_ahs_report.py from directory reports, and get to this line
with open('message_body.html', 'w+') as f:
I get PermissionError: [Errno 13] Permission denied: 'message_body.html'. I feel like there's something obvious I am missing. What is it? I am running the script thru a Jenkins job as user jenkins and I've never had an issue like this.

Python won't overwrite 777 file

I have a script that is outputting the pid. I change permissions to 777. Every subsequent instance will overwrite this file with the new pid. Despite the 777 permissions, python reports an OS Error that the operation is not permitted unless the user executing the script is the owner of the file. (I can delete/overwrite the file from the shell, of course).
#!/usr/bin/python
import os
import time
f = open("/tmp/test.txt", 'w')
f.write("Hello, file!\n")
os.chmod("/tmp/test.txt", 0777)
f.close()
$ /tmp/myscript.py # fine
$ sudo -u other_user /tmp/myscript.py #not fine -- gives error
In Linux, only the user who created the file or root can change its permissions. If the file doesn't exist when you call open, you are the owner of the new file and can change its permissions. When the file already exists, it's just truncated and the existing permissions are still in effect. It would be reasonable to just catch and ignore the error because it only happens after the permissions were setup correctly.
If you have the proper permissions in the parent directory, you can delete the file and create a new one each time. But that doesn't work for /tmp because the sticky bit is set and only owner can delete the file.
$ /tmp/myscript.py creates a file owned by you.
and $ sudo -u other_user /tmp/myscript.py means other_user is going to change the file permission owned by you.
same as the following situation:
account1$ touch /tmp/test.txt
account1$ chmod 777 /tmp/test.txt
su account2
account2$ chmod 777 /tmp/test.txt
#chmod: changing permissions of `test.txt': Operation not permitted

Python CGI permissions error

I can't seem to open and write to a text file from my CGI script.. I keep getting permissions errors (which I see from CGITb)
Whenever I try to do this:
f = open("/Users/varatis/Documents/data.txt","a+")
I get this:
<type 'exceptions.IOError'>: [Errno 13] Permission denied: '/Users/varatis/Documents/data.txt'
args = (13, 'Permission denied')
errno = 13
filename = '/Users/varatis/Documents/data.txt'
message = ''
strerror = 'Permission denied'
Ideally, I'd want to create the text file. But to make things easier for the server, I've tried making things easier by making a premade data.txt, and running the usual:
chmod a+x data.txt
chmod a+w data.txt
chown 70 data.txt (70 is the uid the server runs on, obtained by os.getuid from the os module)
however, these don't even seem to work. Does anyone have experience with this and can help me? Again, ideally I'm appending to the text file from the CGI script and creating it if it doesn't exist.
Also, maybe of use will be the ls -la for the data.txt:
-rwxrwxrwx# 1 _www staff 0 Mar 12 16:18 data.txt
Very likely your webserver doesn't have permissions to write to that specific directory.
If it's an apache then you might want to fiddle around with 'Directory' directive.
update:
What are the permissions on the Documents directory?

Categories