I have a python Lambda that returns this error. This function only read a json event and parse some fields. Stop. I can't paste here the lambda code. This lambda has always worked and with no changes stopped!!!
Lambda code is taken from a BitBucket repository.
Have you by any chance updated the .zip package for lambda? The error might have something to do with the following:
The zip archive preserves file permissions, so if you have a 644
permissions file, deflate it and inflate it back up, you get 644
permissions for that file.
Try setting the expected permissions before deflation, in Lambda's case, 755 should do just fine.
Or... that JSON file the lambda reads might actually be in a folder and you're trying to read it.
Error 13 usually means you are trying to open a file, but your path is a folder.
Another possible reason for this might be the permission issues on the Lambda handler file.
Try this:
chmod 644 your_lambda_handler_file.py
And in the directory where your lambda code is, try this:
chmod 644 $(find . -type f)
chmod 755 $(find . -type d)
Related
In a python 3 script I'm trying to add execution permissions to all .sh files in a directory, as follows:
from os import chmod
chmod('/path_to_dir/dir_prefix_*/bin/*.sh',0o755)
But I'm getting an error:
FileNotFoundError: [Errno 2] No such file or directory:'/path_to_dir/dir_prefix_*/bin/*.sh'
If I run this chmod from bash, it works ok, so I guess python's chmod does not like the use of * in the path.
What would be the correct way to chmod all .sh files in a directory then?
Just use a for loop.
import os
for file in os.listdir("/mydir"):
if file.endswith(".sh"):
chmod(file, mode)
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.)
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
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 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?