How to use copyfile when there are spaces in the directory name?

喜欢而已 提交于 2019-11-30 15:35:44

I don't think spaces are to blame. You have to escape backslashes in paths, like this:

source = 'C:\\Documents and Settings\\Some directory\\My file.txt'

or, even better, use the r prefix:

source = r'C:\Documents and Settings\Some directory\My file.txt'

Use forward slashes or a r'raw string'.

Copyfile handles space'd filenames.

You are not escaping the \ in the file paths correctly.

import shutils

source = 'C:\\Documents and Settings\\Some directory\\My file.txt'
destination = 'C:\\Documents and Settings\\Some other directory\\Copy.txt'

shutil.copyfile(source, destination)

To illustrate, try running this:

print 'Incorrect: C:\Test\Derp.txt'
print 'Correct  : C:\\Test\\Derp.txt'

It seems there are other issues as well. Errno 22 indicates another problem. I've seen this error in the following scenarios:

  • Source file or target file is in use by another process.
  • File path contains fancy Unicode characters.
  • Other access problems.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!