我正在嘗試編寫一個將文件從一個目錄移動到另一個目錄的python腳本。我試過兩種不同的解決方案,都以錯誤結尾。用python移動文件(Windows)
第一:
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
( 「爲文件名,目錄名或卷標語法錯誤」)
號之二:
import shutil
def move(src, dest):
shutil.move(src, dest)
src='C:\users\python\nonpython\Adam.spc'
dest='C:\users\python\target\Adam.spc'
move(src,dest)
給出錯誤
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'
我應該怎麼做才能做到這一點?
r是什麼意思? – user2536262
原始字符串 - https://docs.python.org/2/reference/lexical_analysis.html#string-literals – WeaselFox