Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using shutil.copyfile I get a Python IOError: [Errno 13] Permission denied:

Tags:

I have some python code using shutil.copyfile:

import os import shutil  src='C:\Documents and Settings\user\Desktop\FilesPy' des='C:\Documents and Settings\user\Desktop\\tryPy\Output'  x=os.listdir(src) a=os.path.join(src,x[1])  shutil.copyfile(a,des) print a 

It gives me an error:

IOError: [Errno 13] Permission denied: 'C:\\Documents and Settings\\user\\Desktop\\tryPy\\Output' 

Why don't I have permission to copy the file?

like image 849
DrDark Avatar asked Jun 30 '12 22:06

DrDark


People also ask

How do I fix permission is denied in Python?

The PermissionError: [errno 13] permission denied error occurs when you try to access a file from Python without having the necessary permissions. To fix this error, use the chmod or chown command to change the permissions of the file so that the right user and/or group can access the file.

What is the error No 13 in python?

(13) Permission Denied. Error 13 indicates a filesystem permissions problem. That is, Apache was denied access to a file or directory due to incorrect permissions.

What is Shutil Copyfile?

The shutil. copyfile() method in Python is used to copy the content of the source file to the destination file. The metadata of the file is not copied. Source and destination must represent a file and destination must be writable.

How do you copy an entire directory in Python?

copytree() method recursively copies an entire directory tree rooted at source (src) to the destination directory. The destination directory, named by (dst) must not already exist. It will be created during copying.


2 Answers

From the documentation of shutil.copyfile:

Copy the contents (no metadata) of the file named src to a file named dst. dst must be the complete target file name; look at shutil.copy() for a copy that accepts a target directory path. If src and dst are the same files, Error is raised. The destination location must be writable; otherwise, an IOError exception will be raised. If dst already exists, it will be replaced. Special files such as character or block devices and pipes cannot be copied with this function. src and dst are path names given as strings.

So I guess you need to either use shutil.copy or add the file name to des:

des = os.path.join(des, x[1]) 
like image 58
Lev Levitsky Avatar answered Sep 24 '22 18:09

Lev Levitsky


I advice you rather use shutil.copyfile rather than shutil.copy if you can.

With shutil.copyfile, you have to consider metadata such as writing permission.

like image 41
White Avatar answered Sep 25 '22 18:09

White