2

我有一个非常简单的代码,但由于某种原因,我得到 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'

我真的不知道该怎么办尝试重新搜索整个网络没有得到答案请帮助我。

谢谢 :)

编辑: 这是在 Eclipse 上运行的完整跟踪:

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
4

1 回答 1

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"
于 2012-07-03T02:12:24.933 回答