我想用python脚本删除一些文件(使用Windows时).我试过以下代码:
>>>import os
>>> os.remove ('D:\new.docx')
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误:
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'
Run Code Online (Sandbox Code Playgroud)
这里的任何人都可以帮我吗?
谢谢.
吉拉尼
一些选择:
逃离backslash:
>>> os.remove('D:\\new.docx')
Run Code Online (Sandbox Code Playgroud)
Windows中的运行时库接受a forward slash作为分隔符:
>>> os.remove('D:/new.docx')
Run Code Online (Sandbox Code Playgroud)
>>> os.remove(r'D:\new.docx')
Run Code Online (Sandbox Code Playgroud)