views:

99

answers:

3

Hi,

I want to delete some files with python scripts (while using Windows). I have tried the following code:

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

but I am getting the following error:

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'

Can anyone here help me with this?

THanks.

Gillani

+6  A: 

\ is the escape char for python. try replacing it with \\ .

ex:

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

Thanks Alterlife.......!

It worked.......!

THanks for ur help!!

Regards, Gillani

Gillani
Glad to help :-) . You should probably have used the comments box to respond. People tend to respond badly to comments left in the answers section.
Alterlife
lol....I will take care of it in future...!
Gillani
+5  A: 

A few options:

Escape the backslash:

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

The runtime library in Windows accepts a forward slash as a separator:

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

Raw string:

>>> os.remove(r'D:\new.docx')
gimel
thanks.............!
Gillani