IOError:[Errno 2]没有这样的文件或目录shutil

时间:2012-07-02 21:01:39

标签: python shutil

我有一个非常简单的代码但由于某种原因我得到Err no 2 这是代码:

import os, shutil

src=r"C:/Documents and Settings/user/Desktop/FilesPy"
des=r"C:/Documents and Settings/user/Desktop/tryPy/Output"
srcFile=r"C:/Documents and Settings/user/Desktop/tryPy/Input/FilesToCopy.txt"

srcFile=open(srcFile,'a+')

for line in srcFile:
    name=line.rstrip()
    pathS=os.path.join(src,name)
    pathD=os.path.join(des,name)
    if os.path.exists(pathS):
        shutil.copy(pathS,des)

    else:
        print 'false' + path

但我得到的是:

IOError: [Errno 2] No such file or directory: 'C:/Documents and Settings/user/Desktop/tryPy/Output\\blatwo.docx'

我真的不知道该怎么做尝试重新整理网络没有得到答案请帮帮我。

谢谢:)

修改 的 这里是关于日食运行的完整痕迹:

pydev debugger
Traceback (most recent call last):
  File "D:\EasyEclipse-for-Python-1.3.1\plugins\org.python.pydev.debug_1.3.13\pysrc\pydevd.py", line 803, in <module>
    debugger.run(setup['file'], None, None)
  File "D:\EasyEclipse-for-Python-1.3.1\plugins\org.python.pydev.debug_1.3.13\pysrc\pydevd.py", line 655, in run
    execfile(file, globals, locals) #execute the script
  File "D:\Python\CopyChosenFiles\Copy of CopyFiles.py", line 16, in <module>
    shutil.copy2(pathS,pathD)
  File "C:\Python27\Lib\shutil.py", line 128, in copy2
    copyfile(src, dst)
  File "C:\Python27\Lib\shutil.py", line 83, in copyfile
    with open(dst, 'rb') as fdst:
IOError: [Errno 2] No such file or directory: 'C:/Documents and Settings/user/Desktop/tryPy/Output\\blatwo.docx'
Exception AttributeError: "'NoneType' object has no attribute 'print_exc'" in <function _remove at 0x00AC52B0> ignored

1 个答案:

答案 0 :(得分:2)

用以下代码替换变量赋值:

src = os.path.join('C:', 'Documents and Settings', 'user', 'Desktop', 'FilesPy')
des = os.path.join('C:', 'Documents and Settings', 'user', 'Desktop', 'tryPy', 'Output')
srcFile = os.path.join('C:', 'Documents and Settings', 'user', 'Desktop', 'tryPy', 'Input', 'FilesToCopy.txt')

或者,在路径中使用反斜杠,因为您正在使用Windows ...

src = r"C:\Documents and Settings\user\Desktop\FilesPy"
des = r"C:\Documents and Settings\user\Desktop\tryPy\Output"
srcFile = r"C:\Documents and Settings\user\Desktop\tryPy\Input\FilesToCopy.txt"