how to elevate python file - python

Today I was working on a python script that will copy a folder in one directory and paste it in another directory.Everything was going fine but when I ran my code,It gives me an eror masseges like this:
Traceback (most recent call last):
File "c:\Users\sudam\OneDrive\Desktop\programming\python\projects\file_syncer\syncer.py", line 9, in <module>
shutil.copy(pearent_directry , moving_directry)
File "C:\Users\sudam\AppData\Local\Programs\Python\Python310\lib\shutil.py", line 417, in copy
copyfile(src, dst, follow_symlinks=follow_symlinks)
File "C:\Users\sudam\AppData\Local\Programs\Python\Python310\lib\shutil.py", line 254, in copyfile
with open(src, 'rb') as fsrc:
PermissionError: [Errno 13] Permission denied: 'C:/Users/sudam/OneDrive/Documents/Rainmeter'
and here is my code:
print('file syncer version 1.0')
import shutil
from elevate import elevate
elevate()
pearent_directry = 'C:/Users/sudam/OneDrive/Documents/Rainmeter'
moving_directry = 'D:/sync'
shutil.copy(pearent_directry , moving_directry)
print('process has finished with error code:0')
any yes,I did use elevate to try to get admin rights but,It doesn't work either.
Can somebody please help me out in solving this issue?

Bro, it's easier to change the code to fix your problem.
from distutils.dir_util import copy_tree
a = "The path to the file"
b = "The path to the file"
copy_tree(a, b)
print("Process finished")
Also read the PEP8 documentation.

Related

PermissionError: [Errno 13] Permission denied - Python cannot write/open/read any text or image files

This stuff was ok until recently and I've searched everywhere.
I'm trying to get an image from the internet and then download it.
I'm using python on Windows installed via Anaconda. THe same error occurs on powershell as well as anaconda prompt.
My code (test.py):
from PIL import Image
import requests
from io import BytesIO
import cv2
import numpy
img_url = 'https://d.newsweek.com/en/full/520858/supermoon-moon-smartphone-photo-picture.jpg'
response = requests.get(img_url)
img = Image.open(BytesIO(response.content))
uid = 1
fname = 'test'
x_start, y_start, x_end, y_end = 0,0,10,10
crp = img.crop((x_start, y_start, x_end, y_end))
crp.save('test.jpg')
The error (when running python test.py in console):
Traceback (most recent call last):
File ".\imgtest.py", line 16, in <module>
crp.save('test.jpg')
File "C:\ProgramData\Anaconda3\lib\site-packages\PIL\Image.py", line 2148, in save
fp = builtins.open(filename, "w+b")
PermissionError: [Errno 13] Permission denied: 'test.jpg'
Things I've tried
Changing permissions of the folder that the file is in
Running command prompt as administrator
Tested creating a text file as well using f = open("demo.txt", "w"). The error this time is:
Traceback (most recent call last):
File ".\imgtest.py", line 15, in
f = open("demo.txt", "w")
PermissionError: [Errno 13] Permission denied: 'demo.txt'
From the documentation, try to pass the format as a param:
crp.save('test', "jpg")
or
crp.save('test', "JPEG")
Also from the documentation, Your IOError Raise when:
the file could not be written. The file may have been created and may contain partial data.

Downloading a file but getting permission errors

I'm trying to download a file but when it's trying to write to the current directory it gives a permission error
Traceback (most recent call last):
File "C:\Users\HP User\Desktop\WWE Tool\MasterDownload.py", line 22, in <module>
with open(x, 'wb') as f:
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\HP User\\Desktop\\WWE Tool'
Code:
MasterDownload = requests.get(url=Master, headers=Heads)
fpath = os.getcwd()
with open(fpath, 'wb') as f:
f.write(MasterDownload.content)
I checked the current path and eveything looks fine, I just can't get around as to why it's not writing as I am an admin
You're actually trying to write to a directory (the process' current working directory - as obtained from os.getcwd()), not to a file. Try selecting an actual file in that directory to write to instead of the directory itself, and the issue might go away.

Errno 22 invaild (wb) or filename. Python shutil.copy

I'm beginner in python and I have a problem with this script:
import errno import shutil import os
def copystuff(src, dest):
if os.path.isfile(src):
dest_dirname = os.path.dirname(src)
if not os.path.isdir(dest_dirname):
os.makedirs(dest_dirname)
shutil.copy2(src, dest)
else:
shutil.copytree(src, dest)
copystuff('C:\\Downloads\\index.html', 'J:\\include\\')
Where J it's FlashDriveUSB, and I'm using Python 2.7.
When I'm launching this I got something like that:
C:\Python27>python copy_file.py
Traceback (most recent call last):
File "copy_file.py", line 24, in <module>
copystuff('C:\\Downloads\index.html', 'D:\\include\\')
File "copy_file.py", line 20, in copystuff
shutil.copy2(src, dest)
File "C:\Python27\lib\shutil.py", line 127, in copy2
copyfile(src, dst)
File "C:\Python27\lib\shutil.py", line 82, in copyfile
with open(dst, 'wb') as fdst:
IOError: [Errno 22] invalid mode ('wb') or filename: 'D:\\include\\'
Please help me.
I resolved my problem. I wanted copy file to folder witch doesn't exist so i added to code a few lines checking if inputted path exists and if not path is created.

Removing a tree on a USB device in Python

I've used the following code to remove a tree on a USB device however I'm receiving an OSError:
I also ran the code with sudo python.
import shutil
import os
src = "/media/device/my_folder"
if os.path.exists(dst):
shutil.rmtree(dst)
I've just used shutil.copytree(src, dst) in another script to write the files to the device in the first place. However the USB device was removed during the copy, this is probably causing the issue I'm having as all other files except the one that was half copied have been removed okay.
I'm getting the following traceback:
Traceback (most recent call last):
File "writetousb/tests/deleteTest.py", line 32, in <module>
shutil.rmtree(src)
File "/usr/lib/python2.7/shutil.py", line 252, in rmtree
onerror(os.remove, fullname, sys.exc_info())
File "/usr/lib/python2.7/shutil.py", line 250, in rmtree
os.remove(fullname)
OSError: [Errno 30] Read-only file system: '/media/device/21823/21916.jpg'
So I'm guessing I'll need to change the permissions of the folder and it's files before I remove them?
If I use chmod to set the permissions correctly before I try to use shutil.rmtree then it should work. I'm going to test this and provide an update when I know it works.
I can confirm the solution works.
import shutil
import os
src = "/media/device/my_folder"
if os.path.exists(dst):
os.chmod(dst, 0o777)
for root,dirs,_ in os.walk(dst):
for d in dirs :
os.chmod(os.path.join(root,d) , 0o777)
shutil.rmtree(dst)

Script not working since windows 7

I have the following script which worked fine on XP, since I have a new PC on Windows 7 Professional the code has stopped working
import os
import shutil
from time import strftime
logsdir="c:\logs"
zipdir="c:\logs\puttylogs\zipped_logs"
zip_program="zip.exe"
for files in os.listdir(logsdir):
if files.endswith(".log"):
files1=files+"."+strftime("%Y-%m-%d")+".zip"
os.chdir(logsdir)
os.system(zip_program + " " + files1 +" "+ files)
shutil.move(files1, zipdir)
os.remove(files)
The error I am getting is
U:>python logs.py
zip warning: name not matched: ping_dms_155.log
zip error: Nothing to do! (ping_dms_155.log.2013-05-14.zip)
Traceback (most recent call last):
File "logs.py", line 24, in <module>
shutil.move(files1, zipdir)
File "c:\python27\lib\shutil.py", line 301, in move
copy2(src, real_dst)
File "c:\python27\lib\shutil.py", line 130, in copy2
copyfile(src, dst)
File "c:\python27\lib\shutil.py", line 82, in copyfile
with open(src, 'rb') as fsrc:
IOError: [Errno 2] No such file or directory: 'ping_dms_155.log.2013-05-14.zip'
I can't think why it would stop working, thanks in advance
It seems you do have zip.exe on your windows 7 machine from the error, but it may not be a version compatible with Windows 7.
Check in logsdir to see if the file you modify (ping_dms_155.log.2013-05-14.zip) already exists. If all of these are true I would suggest using python module zipfile.
Your path strings aren't escaped properly.
logsdir="c:\logs"
zipdir="c:\logs\puttylogs\zipped_logs"
should be:
logsdir=r"c:\logs"
zipdir=r"c:\logs\puttylogs\zipped_logs"
The directory c:ogs doesn't exist. Running it manually worked because you changed to the log directory. It ran on XP because... well, you didn't run this exact script on XP because it wouldn't work there either.
I have got this to work by changing the os.system to subprocess so the code now looks like
import os
import shutil
from time import strftime
import subprocess
logsdir="c:\logs"
zipdir="c:\logs\puttylogs\zipped_logs"
zip_program="zip.exe"
for files in os.listdir(logsdir):
if files.endswith(".log"):
files1=files+"."+strftime("%Y%m%d")+".zip"
os.chdir(logsdir)
subprocess.call([zip_program,files1, files])
shutil.move(files1, zipdir)
os.remove(files)

Categories