I've been getting an error while trying to use shutil.copy()
What I'm doing is copying a file from one folder to the other, after checking if that file already exists in the destination folder and deleting it:
if os.path.isfile("C:/folder1/file1.csv"):
full_file_name = os.path.join("C:/folder1/", "file1.csv")
if os.path.isfile("C:/folder2/file1.csv"):
os.remove("C:/folder2/file1.csv")
os.chmod("C:/folder2/",0777)
if os.path.isfile(full_file_name):
shutil.copy(full_file_name, "C:/folder2/")
The error I've been getting is:
Traceback (most recent call last):
File "script1.py", line 55, in <module>
File "shutil.pyc", line 119, in copy
File "shutil.pyc", line 82, in copyfile
IOError: [Errno 13] Permission denied: 'C:/folder1/file1.csv'
What could the problem be?
The important part is:
Permission denied: 'C:/folder1/file1.csv'
Which means it cannot access file1.csv. Check this file is readable by the user running the script.
Related
When I try to use the runner.py example via the command line, I encounter the following error:
C:\Program Files (x86)\Eclipse\Sumo\doc\tutorial\traci_tls>runner.py
Traceback (most recent call last):
File "C:\Program Files (x86)\Eclipse\Sumo\doc\tutorial\traci_tls\runner.py", line 122, in
generate_routefile()
File "C:\Program Files (x86)\Eclipse\Sumo\doc\tutorial\traci_tls\runner.py", line 47, in generate_routefile
with open("data/cross.rou.xml", "w") as routes:
PermissionError: [Errno 13] Permission denied: 'data/cross.rou.xml'
Who knows how to solve this error?
Regards, Ali
Please make a local copy of the tutorial to your home directory before running it. You probably do not have write access to the installation directory.
This is my code:
import shutil
original = r'C:\Users\Руслан\Desktop\HACKPROG\IP.PY'
target = r'C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp'
shutil.move(original,target)
and I am getting the following Error:
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:\Users\Руслан\Desktop\HACKPROG\IP.PY", line 24, in <module>
shutil.move(original,target)
File "C:\Users\Руслан\AppData\Local\Programs\Python\Python39\lib\shutil.py", line 820, in move
copy_function(src, real_dst)
File "C:\Users\Руслан\AppData\Local\Programs\Python\Python39\lib\shutil.py", line 435, in copy2
copyfile(src, dst, follow_symlinks=follow_symlinks)
File "C:\Users\Руслан\AppData\Local\Programs\Python\Python39\lib\shutil.py", line 264, in copyfile
with open(src, 'rb') as fsrc, open(dst, 'wb') as fdst:
PermissionError: [Errno 13] Permission denied: 'C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\StartUp\\IP.PY'
You don't appear to have permissions to write to the Start Up folder.
You can fix this by running your Python terminal as an administrator (right click IDLE/cmd and press run as administrator).
you can run this program in 2 ways
Method 1:
Add python to path, By editing environmental variables.
Method 2:
Open console/cmd . Direct path to python file by
cd C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\StartUp
And then after entering directory
python ip.py --user
This is the code I executed:
shutil.copy(r'd:\try',r'd:\new')
The output I got was:
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
shutil.copy(r'd:\try',r'd:\new')
File "C:\Users\Username\AppData\Local\Programs\Python\Python38-32\lib\shutil.py", line 415, in copy
copyfile(src, dst, follow_symlinks=follow_symlinks)
File "C:\Users\username\AppData\Local\Programs\Python\Python38-32\lib\shutil.py", line 261, in copyfile
with open(src, 'rb') as fsrc, open(dst, 'wb') as fdst:
PermissionError: [Errno 13] Permission denied: 'd:\\try'
I get this permission error only when I try to copy folders but this error doesn't occur when I copy a file. How to fix this?
As can be read in the documentation, shutil.copy() is used for copying files.
To copy a directory, use shutil.copytree() instead:
shutil.copytree(r'd:\try', r'd:\new')
I am trying to write a python script to copy some files from one place to another like this:
os.system('cp file/path/a file/path/b')
when I run this within the python prompt it works just fine. But if I put this in a python script it fails with the error
cp: cannot create regular file 'file/path/b': Permission denied
I am getting this error even when I run the script using sudo permissions i.e. sudo ./mysript.py
So what is going on here and is it possible for me to fix this?
EDIT 1:
Following the suggestions of #kungphu and #ShadowRanger I switched from using os.system to using shutil.copy. The problem persists however I am now getting a different error:
Traceback (most recent call last):
File "./myscript.py", line 21, in <module>
shutil.copy('/absolute/file/path/a', '/absolute/file/path/b')
File "/python/lib/python2.6/shutil.py", line 84, in copy
copyfile(src, dst)
File "/python/lib/python2.6/shutil.py", line 50, in copyfile
with open(src, 'rb') as fsrc:
IOError: [Errno 13] Permission denied: '/absolute/file/path/a'
I'm trying to run this dumper script I have, but every time I try to run it in IDLE, it gives me this error:
Traceback (most recent call last):
File "C:\Python27\Python_Scripts\dumper.py", line 282, in <module>
cat=Cat(catName)
File "C:\Python27\Python_Scripts\dumper.py", line 47, in __init__
cat2=open(catname,"rb")
IOError: [Errno 13] Permission denied: 'C:/Program Files (x86)/Origin Games/Plants vs Zombies Garden Warfare 2'
>>>
I even ran as administrator through command line on Windows 7, but no luck on that end either! If anyone has any idea on what the problem is, let me know. I can provide the full .py file in case that helps.
Bryce's suspicion is correct.
>>> open('C:/programs', 'rb') # a directory
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
open('C:/programs', 'rb')
PermissionError: [Errno 13] Permission denied: 'C:/programs'