Python Forum
delete all files which contains word sql_Table1
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
delete all files which contains word sql_Table1
#1
Hi Team,

how to delete below files from a folder
which contains word sql_Table1


sql_Table1.csv
sql_Table1.gzip
Chksum_sql_Table1_20220806

import os
import os.path

def dbs_remove(folderpath,table)

	file = f'{tbl}.csv'
	fname = os.path.join(folderpath,tbl)
	print(file)

	if os.path.exists(fname):
		os.remove(fname)
	else:
		print('Path not found')



if __name__ == "__main__":
	#tbl = "sql_Table1"
	tbl = input("Enter TableName")
	folderpath = "E:\\test\reports"
Yoriz write Sep-15-2022, 11:57 AM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
Use pathlib:
from pathlib import Path


def remove_files(root, pattern):
    for element in Path(root).rglob(pattern):
        if element.is_file():
            element.unlink()


if __name__ == "__main__":
    tbl = "*sql_Table1*"
    # using the wildcard for rglob()
    folderpath = "E:\\test\reports"
    remove_files(folderpath, tbl)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
Hi DeaD_EyE,

Superb ! Thumbs Up your code is working as expected ! thanks for your help.



Thanks
mg
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help replacing word in Mutiple files. (SOLVED) mm309d 0 843 Mar-21-2023, 03:43 AM
Last Post: mm309d
  python print all files which contain specific word in it mg24 5 1,261 Jan-27-2023, 11:20 AM
Last Post: snippsat
  delete all files and subdirectory from a main folder mg24 7 1,637 Oct-28-2022, 07:55 AM
Last Post: ibreeden
  Delete empty text files [SOLVED] AlphaInc 5 1,579 Jul-09-2022, 02:15 PM
Last Post: DeaD_EyE
  Delete files from amazon s3 bucket python_student 0 3,932 Jan-14-2022, 04:51 PM
Last Post: python_student
Question Problem: Check if a list contains a word and then continue with the next word Mangono 2 2,518 Aug-12-2021, 04:25 PM
Last Post: palladium
  Searching for specific word in text files. JellyCreeper6 1 1,748 Nov-03-2020, 01:52 PM
Last Post: DeaD_EyE
  Complex word search multiple files Kristenl2784 0 1,589 Jul-18-2020, 01:22 PM
Last Post: Kristenl2784
  Python Speech recognition, word by word AceScottie 6 16,027 Apr-12-2020, 09:50 AM
Last Post: vinayakdhage
  print a word after specific word search evilcode1 8 4,866 Oct-22-2019, 08:08 AM
Last Post: newbieAuggie2019

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020