2009-09-16 64 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')