2

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

2 Answers 2

6

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')
Sign up to request clarification or add additional context in comments.

Comments

6

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

ex:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.