Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.7k views
in Technique[技术] by (71.8m points)

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

I have some python code using shutil.copyfile:

import os
import shutil

src='C:Documents and SettingsuserDesktopFilesPy'
des='C:Documents and SettingsuserDesktop\tryPyOutput'

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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])

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...