2009-09-16 95 views
2

我想用python腳本刪除一些文件(使用Windows時)。我曾嘗試下面的代碼:使用python腳本刪除文件

>>>import os 
>>> os.remove ('D:\new.docx') 

,但我收到以下錯誤:

Traceback (most recent call last): 

    File "<pyshell#1>", line 1, in -toplevel- 

    os.remove ('D:\new.docx') 
OSError: [Errno 22] Invalid argument: 'D:\new.docx' 

這裏任何人能幫助我嗎?

THanks。

吉拉尼

回答

6

有幾個選項:

逃離backslash

>>> os.remove('D:\\new.docx') 

在Windows運行時庫接受forward slash作爲分隔符:

>>> os.remove('D:/new.docx') 

Raw string

>>> os.remove(r'D:\new.docx') 
+0

thanks .............! – Gillani 2009-09-16 11:26:47

6

\是蟒蛇逃脫字符。嘗試用\\替換它。

例如:

os.remove ('D:\\new.docx')