Python. IOError: [Errno 13] Permission denied: when i'm copying file

Question:

I have two folders: In, Out – it is not system folder on disk D: – Windows 7. Out contain “myfile.txt” I run the following command in python:

>>> shutil.copyfile( r"d:Outmyfile.txt", r"D:In" )

Traceback (most recent call last):
  File "<pyshell#39>", line 1, in <module>
    shutil.copyfile( r"d:Outmyfile.txt", r"D:In" )
  File "C:Python27libshutil.py", line 82, in copyfile
    with open(dst, 'wb') as fdst:
IOError: [Errno 13] Permission denied: 'D:\In'

What’s the problem?

Asked By: G-71

||

Answers:

Read the docs:

shutil.copyfile(src, dst)

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 copy()
for a copy that accepts a target directory path.

Answered By: Tim Pietzcker

use
shutil.copy instead of shutil.copyfile

example:

shutil.copy(PathOf_SourceFileName.extension,TargetFolderPath)
Answered By: sohom

well the questionis old, for new viewer of Python 3.6
use

shutil.copyfile( "D:Outmyfile.txt", "D:In" )

instead of

shutil.copyfile( r"d:Outmyfile.txt", r"D:In" )

r argument is passed for reading file not for copying

Answered By: abhinav

I solved this problem, you should be the complete target file name for destination

destination = pathdirectory + filename.*

I use this code fir copy wav file with shutil :

    # open file with QFileDialog

    browse_file = QFileDialog.getOpenFileName(None, 'Open file', 'c:', "wav files (*.wav)")

    # get file name 

    base = os.path.basename(browse_file[0])
    os.path.splitext(base)
    print(os.path.splitext(base)[1])

    # make destination path with file name

    destination= "test/" + os.path.splitext(base)[0] + os.path.splitext(base)[1]
    shutil.copyfile(browse_file[0], destination)
Answered By: hassanzadeh.sd

Use shutil.copy2 instead of shutil.copyfile

import shutil 
shutil.copy2('/src/dir/file.ext','/dst/dir/newname.ext') # file copy to another file
shutil.copy2('/src/file.ext', '/dst/dir') # file copy to diff directory
Answered By: Gowtham Balusamy

use

> from shutil import copyfile
> 
> copyfile(src, dst)

for src and dst use:

srcname = os.path.join(src, name)
dstname = os.path.join(dst, name)
Answered By: msnmkh

First of all, make sure that your files aren’t locked by Windows, some applications, like MS Office, locks the oppened files.

I got erro 13 when i was is trying to rename a long file list in a directory, but Python was trying to rename some folders that was at the same path of my files. So, if you are not using shutil library, check if it is a directory or file!

import os
path="abc.txt"

if os.path.isfile(path):
    #do yor copy here
    print("nIt is a normal file") 

Or

if os.path.isdir(path):
    print("It is a directory!")
else:
    #do yor copy here
    print("It is a file!")
Answered By: Hugo Vares

This works for me:

import os
import shutil
import random


dir = r'E:/up/2000_img'
output_dir = r'E:/train_test_split/out_dir'


files = [file for file in os.listdir(dir) if os.path.isfile(os.path.join(dir, file))]

if len(files) < 200:
    # for file in files:
    #     shutil.copyfile(os.path.join(dir, file), dst)
    pass
else:
    # Amount of random files you'd like to select
    random_amount = 10
    for x in range(random_amount):
        if len(files) == 0:
            break
        else:
            file = random.choice(files)
            shutil.copyfile(os.path.join(dir, file), os.path.join(output_dir, file))
Answered By: Peyman habibi

Make sure you aren’t in (locked) any of the the files you’re trying to use shutil.copy in.

This should assist in solving your problem

Answered By: Brad Eland

Visual Studio 2019

Solution : Administrator provided full Access to this folder "C:ProgramDataDocker"
it is working.

ERROR: File IO error seen copying files to volume: edgehubdev. Errno: 13, Error Permission denied : [Errno 13] Permission denied: ‘C:ProgramDataDockervolumesedgehubdev_dataedge-chain-ca.cert.pem’
[ERROR]: Failed to run ‘iotedgehubdev start -d "C:Usersradhe.sahsourcerepostestingAzureIotEdgeApp1configdeployment.windows-amd64.json" -v’ with error: WARNING! Using –password via the CLI is insecure. Use –password-stdin.
ERROR: File IO error seen copying files to volume: edgehubdev. Errno: 13, Error Permission denied : [Errno 13] Permission denied: ‘C:ProgramDataDockervolumesedgehubdev_dataedge-chain-ca.cert.pem’

Answered By: RadheSah

I avoid this error by doing this:

1)Import lib ‘pdb’ and insert ‘pdb.set_trace()’ before ‘shutil.copyfile’, it would just like this:

`import pdb

print(dst)

pdb.set_trace()

shutil.copyfile(src,dst)
`

2)run the python file in a terminal, it will execute to the line ‘pdb.set_trace()’, and now the ‘dst’ file will print out.

3)copy the ‘src’ file by myself, and substitute and remove the ‘dst’ file which has been created by the above code.

4)Then input ‘c’ and click the ‘Enter’ key in the terminal to execute the following code.

Answered By: bq long
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.