我有一個日誌文件,並在日誌中,有些文件有錯誤,所以當一個文件有錯誤時,我想讀取它的名字(包括路徑)並將它移動到另一個目錄。shutil.move問題 - 額外的
我可以正確讀取輸入文件,例如其中一個文件是:C:\ test \ test1我可以找到該文件,我只想移動它。但是,當我使用shutil.move(filename,another_directory)時,即使打印文件名顯示爲c:\ test1 \ test1,shutil也會在每個斜槓前添加一個額外的'\'..即它試圖移動C:\\ test1 \\ TEST1。 [Shutil.move通過在每個現有'\'中添加額外'\'來誤讀輸入路徑]
我該如何解決?謝謝 !
import shutil
f=open("test_logs")
o=open("output_logs","w")
e=open("error_logs",'w')
another_directory= "C:/test"
for line in f:
if line.lstrip().startswith("C:"):
filename = line
#print line
#look for errors in the next line onwards.
if line.startswith("error"):
e.write(filename + "\n")
print filename
shutil.move(filename,another_directory)
f.close()
o.close()
這是錯誤我得到 - IO錯誤:[錯誤2]沒有這樣的文件或目錄:「C:\\測試\\ TEST1(該文件是C:\測試\ TEST1)和打印文件名顯示c:\ test \ test1
難道這可能是你忘記逃避`another_directory`中的反斜槓嗎? – Santa 2011-02-14 20:48:19
錯誤是當它通過輸入日誌文件(名稱爲c:\ test1 \ test)時,print filename正確地將輸出顯示爲C:\ test1 \ test,但我得到一個錯誤,說沒有這樣的文件或目錄C「 \\ test1 \\ test from shutil command。 – Illusionist 2011-02-14 20:49:18