Antoine Deleuze
Antoine Deleuze

Reputation: 37

Wildcard error - "invalid option"

I am currently working on a python script in which there is a moment I want to delete a file which name is ending with .txt

To do so I just run a command line using os in python:

os.system("del working/*.txt")

When running the python script, I get the following error in cmd:

Option non valide - "*". which can be translated "Invalid option"

It seems that the wildcard isn't recognized by cmd but I know very little about this. Why is it not working ?

I know I could handle the situation with regular expressions but I'd like to understand.

Thank you in advance

Upvotes: 0

Views: 670

Answers (2)

Thiago Lelis
Thiago Lelis

Reputation: 26

I think its better use os.remove instead os.system with "del" command. Using os.system your script will not work on linux. Here a example using os.remove:

files = os.listdir("working\")
for fi in files:
    if fi.endswith(".json"):
        os.remove("working\{}".fomat(fi))

Upvotes: 1

blhsing
blhsing

Reputation: 106543

In Windows, \ is the path delimiter, not /, so you should do:

os.system(r"del working\*.txt")

Note that / in Windows is for switches, hence the "invalid option" error.

Upvotes: 2

Related Questions