使用python(Windows)移动文件

使用python(Windows)移动文件,python,windows,python-2.7,Python,Windows,Python 2.7,我试图编写一个python脚本,将文件从一个目录移动到另一个目录。我尝试了两种不同的解决方案,都以错误告终 第一: import os os.rename('C:\users\python\nonpython\adam.spc','C:\users\python\target\adam.spc') 出错 Traceback (most recent call last): File "C:/Users/Python/movefile.py", line 4, in <module&

我试图编写一个python脚本,将文件从一个目录移动到另一个目录。我尝试了两种不同的解决方案,都以错误告终

第一:

import os

os.rename('C:\users\python\nonpython\adam.spc','C:\users\python\target\adam.spc')
出错

Traceback (most recent call last):
  File "C:/Users/Python/movefile.py", line 4, in <module>
    os.rename('C:\users\python\nonpython\adam.spc','C:\users\python\target\adam.spc')
WindowsError: [Error 123] Felaktig syntax för filnamn, katalognamn eller volymetikett
Traceback (most recent call last):
  File "C:/Users/Python/movefile2.py", line 9, in <module>
    move(src,dest)
  File "C:/Users/Python/movefile2.py", line 4, in move
    shutil.move(src, dest)
  File "C:\Python27\lib\shutil.py", line 301, in move
    copy2(src, real_dst)
  File "C:\Python27\lib\shutil.py", line 130, in copy2
    copyfile(src, dst)
  File "C:\Python27\lib\shutil.py", line 82, in copyfile
    with open(src, 'rb') as fsrc:
IOError: [Errno 22] invalid mode ('rb') or filename: 'C:\\users\\python\nonpython\\Adam.spc'
出错

Traceback (most recent call last):
  File "C:/Users/Python/movefile.py", line 4, in <module>
    os.rename('C:\users\python\nonpython\adam.spc','C:\users\python\target\adam.spc')
WindowsError: [Error 123] Felaktig syntax för filnamn, katalognamn eller volymetikett
Traceback (most recent call last):
  File "C:/Users/Python/movefile2.py", line 9, in <module>
    move(src,dest)
  File "C:/Users/Python/movefile2.py", line 4, in move
    shutil.move(src, dest)
  File "C:\Python27\lib\shutil.py", line 301, in move
    copy2(src, real_dst)
  File "C:\Python27\lib\shutil.py", line 130, in copy2
    copyfile(src, dst)
  File "C:\Python27\lib\shutil.py", line 82, in copyfile
    with open(src, 'rb') as fsrc:
IOError: [Errno 22] invalid mode ('rb') or filename: 'C:\\users\\python\nonpython\\Adam.spc'
回溯(最近一次呼叫最后一次):
文件“C:/Users/Python/movefile2.py”,第9行,在
移动(src、dest)
文件“C:/Users/Python/movefile2.py”,第4行,在move中
航天飞机移动(src、dest)
文件“C:\Python27\lib\shutil.py”,第301行,移动中
副本2(src、real_dst)
文件“C:\Python27\lib\shutil.py”,第130行,在copy2中
复制文件(src、dst)
copyfile中第82行的文件“C:\Python27\lib\shutil.py”
开放式(src,'rb')作为fsrc:
IOError:[Errno 22]无效模式('rb')或文件名:“C:\\users\\python\nonpython\\Adam.spc”

我应该怎么做才能使它起作用?

尝试使用斜杠而不是反斜杠-将
\
替换为
/
。或者使用
r'C:\users\python\nonpython\adam.spc'

您的目录名应该有斜杠而不是反斜杠

import os

src = 'C:/users/python/nonpython/Adam.spc'
dest = 'C:/users/python/target/Adam.spc'

os.rename(src, dest)

尝试将路径中的反斜杠替换为正斜杠:

os.rename('C:/users/python/nonpython/adam.spc','C:/users/python/target/adam.spc')

修复此问题的3个选项:

  • 使用前斜杠:
'path/to/your/stuff'

  • 或原始字符串:
r'path\to\your\stuff'

  • 或者至少摆脱你的反斜杠:
'path\\to\\your\\stuff'


这需要执行,因为
\
是字符串中的特殊字符,用于指示特殊字符,如
\n
。因此,当您想动态处理路径时,结果会很糟糕。

您不能使用“重命名”将文件移动到另一个目录,但可以使用“移动”重命名文件。移动可以替换现有文件(使用/y),重命名不能

您可以使用斜杠或反斜杠,反斜杠需要在需要1的地方使用2个反斜杠转义,在需要2的地方使用4个反斜杠转义


您的函数move需要返回。

r是什么意思?原始字符串-