Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete files with python through OS shell

Tags:

python

file

Im Tyring to Delete all Files in E:. with wildcard.

E:\test\*.txt

I would ask rather than test the os.walk. In windows.

like image 973
Merlin Avatar asked Apr 03 '11 21:04

Merlin


People also ask

How do I delete a file in Python os?

Using the os module in pythonTo use the os module to delete a file, we first need to import it, then use the remove() function provided by the module to delete the file. It takes the file path as a parameter. You can not just delete a file but also a directory using the os module.

How do you delete a file in Shell?

To delete a specific file, you can use the command rm followed by the name of the file you want to delete (e.g. rm filename ). For example, you can delete the addresses.


1 Answers

The way you would do this is use the glob module:

import glob
import os
for fl in glob.glob("E:\\test\\*.txt"):
    #Do what you want with the file
    os.remove(fl)
like image 194
cwallenpoole Avatar answered Oct 03 '22 22:10

cwallenpoole