How do I zip and unzip files in python? - 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.

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.

PermissionError: [Errno 13] Permission denied: 'saves'

I would like to open a folder with Pycharm but I get an error every time I try ...
I just execute the command:
saves = open ("saves")
I've tried starting it as an administrator but it didn't work
I've tried changing the folder permissions and it didn't work either
I would like help thanks!

"site-packages\PIL\image.py",line 2131, in save...PermissionError:[error 13] permission denied: scr.png

I have made a python code that takes a screenshot and saves it. Am facing the following Error in saving a piece of code:
"site-packages\PIL\image.py",line 2131, in save...PermissionError:[error 13] permission denied: scr.png
Note
Folder permission is set to read/write.
Piece of code:
snapshot = ImageGrab.grab()
# Using png because it cannot write mode RGBA as JPEG
file = "scr.png"
snapshot.save(file)
Move your code to desktop, then your code will automatically work.
Move your code out of Site-Packages to Desktop or somewhere
Try in another directory and put the full path of the image file:
file = "path/to/img/scr.png"
or try with sudo or admin prompt

How to copy file through Shutil python

I was trying to copy file from my desktop to System32 dir. it give me the following error.
PermissionError: [Errno 13] Permission denied:'C:/Windows/System32/abc.exe'
here is my code
src = pth + "\\" +s_name
dist = "C:/Windows/System32/"
shutil.copy(src , dist)
The issue is not with Python or shutil. Permission denied means that the user that runs the Python script does not have a permission to copy the file to C:/Windows/System32.
I think what you could do now is to look for a way to run the script as Windows administrator.

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.

Categories