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?
Related
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.)
So I'm trying to read a config file which needs to be "sudo'd" into but I'm trying to read its contents using python only without using terminal. when I use the code below I get the error as shown:
with open('/etc/motion/motion.conf','rb') as file:
data = file.readlines()
IOError: [Errno 13] Permission denied: '/etc/motion/motion.conf'
Is there anything I could try to read the contents of the file strictly with python?
You should call your python file from a shell script, change the owner of this script to be root and then set the SUID bit of this script. The user will be able to run the script but the owner of the script will be root:
http://www.codecoffee.com/tipsforlinux/articles/028.html
http://www.linuxnix.com/2011/12/suid-set-suid-linuxunix.html
etc.
Of course you must be aware of security issues that may be involved by such a thing but for local use with a simple script doing one thing, it's OK.
Permission denied means that you have no permission to read that file.
If you're on Linux, you should change the file's permission use chmod command. Or you can use sudo comand run python like this:
sudo python filename.py
or login as root then run python:
su - root
python filename.py
And if you want run the program as root every times, you can use os.execlpe() function like this:
import os
import sys
euid = os.geteuid()
if euid != 0:
args = ['sudo', sys.executable] + sys.argv + [os.environ]
os.execlpe('sudo',*args)
with open('/etc/motion/motion.conf','rb') as file:
data = file.readlines()
print data
This solution is from here:
authentication in python script to run as root
and is courtesy of #samplebias
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.
I have the following set-up: I have a disk mounted with sshfs and I am trying to access the files on it from a python script run locally on my machine. The problem is that even though my user doesn't have any rights over a file - i.e.:
-rw------- 1 13912 1313 40 Nov 25 11:04 test_file.txt
(I am not the owner of the file), os.access still returns True:
>> os.access('/path/to/file/test_file.txt', os.R_OK)
>> True
though when I try to open this file, I get a permission denied error - which is the expected behaviour.
>> f=open('/path/to/file/test_file.txt')
>> IOError: [Errno 13] Permission denied: '/path/to/file/test_file.txt'
Can anyone explain me what am I doing wrong or if there is something I am missing regarding os.access? Is it because this file is owned by a user which doesn't exist in my os? ()
Thanks in advance!
I am trying to run a python script which uses a binary file (xFiles.bin.addr_patched) created by a postlinker. However, I am getting this error.
File "abc.py", line 74, in ParseCmd
shutil.copy(gOptions.inputX, gWorkingXFile)
File "/usr/lib/python2.6/shutil.py", line 89, in copy
copymode(src, dst)
File "/usr/lib/python2.6/shutil.py", line 66, in copymode
os.chmod(dst, mode)
OSError: [Errno 1] Operation not permitted: 'myPath/xFiles.bin.addr_patched'
When I checked the permissions of this xFiles.bin, by ls-l, it shows that
-rwxrwxrwx 1 nobody nogroup
I presume the error is because this file was created by some other application, the python script I am running does not have access to it. Since I am beginner wrt ubuntu, I don't really know how to fix it. Any suggestions on how to fix this?
SOLVED:
As one of the answers Suggested : chown username:groupname file name fixes this issue
You could try (from the command line, but I'm sure there's a syntax in python):
sudo chown your_username:your_groupname filename
Note: The group is usually just your username.
I feel like there's something wrong with those permissions though. Read Write Execute for everyone seems to be off. How was this file created? How did it get to be created by the user nobody?
Python code to change the permission:
from getpwnam import pwd
from getgrnam import grp
import os
uid = getpwnam("YOUR_USERNAME")[2]
gid = grp.getgrnam("YOUR_GROUPNAME")[2]
os.chown("myPath/xFiles.bin.addr_patched", uid, gid)
Run the script with sudo and you're done.
I had this problem when running a python script on my mac (10.14 Mojave) trying to access /Users/xxx/Pictures/Photos Library.photoslibrary.
The full solution can be found in http://osxdaily.com/2018/10/09/fix-operation-not-permitted-terminal-error-macos/
Summary:
Go to System Preferences > Security & Privacy > Privacy > Full Disk Access and add your IDE or python interpreter to the list.
My guess is that you should be looking at the permissions for myPath folder instead. Seems like you can't write to it, hence the problem. Try ls -l myPath/.. and see the permissions for myPath. If that's the problem, change the permissions on the folder with chmod.
P.S. Also, see Google top result on Linux file permissions.