How to delete files with specific patterns in python?
Question:
I have a bunch of files in the directory web, I need to match the pattern and delete the appropriate files.The files are in the following order
1520_22.txt,
1530_22.txt,
1540_22.txt,
new_pwd_v1_asia_1530_22.json,
new_pwd_v1_eur_1540_22.json,
new_pwd_v1_aus_1520_22.json
Each json file has a corresponding text file identified by the number for ex: new_pwd_v1_asia_1530_22.json file has 1530_22 in it’s name hence it’s corresponding txt file is 1530_22.txt, similarly for the other two files. How do I delete aus related files ( in this case new_pwd_v1_aus_1520_22.json and 1520_22.txt).
I tried glob.glob module but it’s not able to match the characters properly
glob.glob("/web/*[aus]" , recursive=True)
Answers:
Take it in steps:
Get the filenames into an list
For testing purposes, we can start with your list:
files="""1520_22.txt,
1530_22.txt,
1540_22.txt,
new_pwd_v1_asia_1530_22.json,
new_pwd_v1_eur_1540_22.json,
new_pwd_v1_aus_1520_22.json""".split(",n")
In your case you want to read the directory "live", and you like glob
, so instead of the above, do this:
import glob
files=glob.glob("/web/*", recursive=True)
Use a list comprehension to select relevant files
jsons_to_delete = [file for file in files if "aus" in file]
txts_to_delete = [file[-12:-5]+".txt" for file in jsons_to_delete ]
files_to_delete = jsons_to_delete+ txts_to_delete
print(files_to_delete)
Delete the files
I assume you will have already done import os
.
for file in files_to_delete:
os.remove(file)
I have a bunch of files in the directory web, I need to match the pattern and delete the appropriate files.The files are in the following order
1520_22.txt,
1530_22.txt,
1540_22.txt,
new_pwd_v1_asia_1530_22.json,
new_pwd_v1_eur_1540_22.json,
new_pwd_v1_aus_1520_22.json
Each json file has a corresponding text file identified by the number for ex: new_pwd_v1_asia_1530_22.json file has 1530_22 in it’s name hence it’s corresponding txt file is 1530_22.txt, similarly for the other two files. How do I delete aus related files ( in this case new_pwd_v1_aus_1520_22.json and 1520_22.txt).
I tried glob.glob module but it’s not able to match the characters properly
glob.glob("/web/*[aus]" , recursive=True)
Take it in steps:
Get the filenames into an list
For testing purposes, we can start with your list:
files="""1520_22.txt,
1530_22.txt,
1540_22.txt,
new_pwd_v1_asia_1530_22.json,
new_pwd_v1_eur_1540_22.json,
new_pwd_v1_aus_1520_22.json""".split(",n")
In your case you want to read the directory "live", and you like glob
, so instead of the above, do this:
import glob
files=glob.glob("/web/*", recursive=True)
Use a list comprehension to select relevant files
jsons_to_delete = [file for file in files if "aus" in file]
txts_to_delete = [file[-12:-5]+".txt" for file in jsons_to_delete ]
files_to_delete = jsons_to_delete+ txts_to_delete
print(files_to_delete)
Delete the files
I assume you will have already done import os
.
for file in files_to_delete:
os.remove(file)