PermissionError on writable file in Jenkins - python

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.

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.

Cannot write to a file within python script, on startup

Basically I have a python script which is "converted" to exe, located in C:\Users\USER_NAME\AppData\Roaming\Folder. When started manually (not as admin), the script can write to file (file is located in the same directory as the script). But when the script runs on startup (I added a registry string to Computer\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run with an executable path as data) I get Permission Denied error, why is that so and how could I fix it?. Code for opening file:
file = open(os.path.join(current_path, "text.txt"), "a+")
Error:
PermissionError: [Errno 13] Permission denied: '.\\text.txt'
The issue was that the script was run from C:\Windows\System32 not from C:\Users\USER_NAME\AppData\Roaming\Folder (windows being weird I guess)

celery log shows "Permission Denied"

I'm running celery on Windows, and there's a task that writes a file on a network drive. Looks like it's having issue to write due to permission. Here's part of celery log that shows the error:
File "C:\TaskerApp\FlaskApps\Tasker\tasks.py", line 194, in export
with open(filename, 'wb') as w:
IOError: [Errno 13] Permission denied: u'//saab/Data/5863/5-Message/5863_2M.txt'
And this is the original code:
#celery.task(name="tasks.export")
def export(file_name):
with open(file_name, 'wb') as w:
w.write('test')
However when I try to write the file directly from python command line, I don't see any issue.
Just wonder what is the possible reason to cause this issue?
Finally I got this work. Looks like the service is running under Local System Account, after I share the file path with this local account, the issue went away.

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

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.

How do I zip and unzip files in python?

I am trying to move a folder from one place to another, however I get this error when I try:
PermissionError: [Errno 13] Permission denied
I use the following code:
import os.path, shutil #, zipfile
if os.path.exists("\\\BOGORODITSASERV\\Not.py Files"):
print("#YOLO")
#zip command
shutil.copy2('C:\\Users\\Grant\\Documents\\AllDerProgramming','\\\BOGORODITSASERV\\Not.py Files')
#unzip command
print("Success!")
EDIT: I have made sure that the file is not open/being used. So to get around this error I was thinking zipping and unzipping the file would work.
EDIT:How then, if not by zipping, do I get rid if this error? Can I give python permission to this file path?
EDIT:I run this program on an admin account, I even run the shell as administrator and I still get the permission error.

Categories