Python Forum
python print all files which contain specific word in it
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python print all files which contain specific word in it
#1
Hi Team,

how to print all files which contains specific word in files.
how to concat source path and word to search for.


import glob
SOURCE_PATH = r"E:\data\src"

with open(r'E:\data\mobile_list.csv','r') as f:
    reader = csv.reader(f)
    header = next(reader)
    for m in reader:
        files = glob.glob(SOURCE_PATH+r'\'+ f'*{m}*')
Reply
#2
Hi Team,

I am trying to move files from one location to another location.

I want to move few files, by filtering ,
existing code moving all files, how to use glob module correctly.


import glob
import os
import shutil
import csv

SOURCE_PATH = r"E:\data\src"
TARGET_PATH = r"E:\data\destination"


with open(r'E:\data\mobile_list.csv','r') as f:
    reader = csv.reader(f)
    header = next(reader)
    for m in reader:
        all_files = glob.glob(os.path.join(SOURCE_PATH, f'*{m}*'))
        for file in all_files:
            shutil.move(file, TARGET_PATH)
            print("finished")
Reply
#3
You should really use pathlib. It is much better than manipulating paths using os.

I think you are using glob wrong. Instead of making a pattern that includes the path, I think you should specify the path using the root_dir parameter.
Reply
#4
As mention pathlib is a better choice,for search a word i would prefer regex rather than glob.
pathlib has own glob Path.glob.
To give a example with regex as mention,this search for word life in filename.
from pathlib import Path
import re

my_dir = r'C:\code\a_folder'
pattern = re.compile(r'life')
for file in Path(my_dir).iterdir():
    if file.is_file:
        if re.search(pattern, file.stem):
            print(file)
Output:
C:\code\a_folder\life.py C:\code\a_folder\life.txt C:\code\a_folder\life_Kopi.py C:\code\a_folder\meaninglife.py
Also see that pathlih always give full path back,then do not need too use join() to get full path.
A stricter search can eg add \b to do a whole words only search.
pattern = re.compile(r'\blife\b')
Output:
C:\code\a_folder\life.py C:\code\a_folder\life.txt
Reply
#5
Hi Deanhystad,

I liked your solution, one more help
LF = 'life'

how to pass LF variable in below syntax.

pattern = re.compile(r'\blife\b')


Thanks
mg
Reply
#6
(Jan-27-2023, 07:20 AM)mg24 Wrote: Hi Deanhystad,

I liked your solution, one more help
A little mixup in names as you talking about code in my post Wink
Like this.
LF = 'life'
pattern = re.compile(fr'\b{LF}\b')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help replacing word in Mutiple files. (SOLVED) mm309d 0 797 Mar-21-2023, 03:43 AM
Last Post: mm309d
  python move specific files from source to destination including duplicates mg24 3 1,051 Jan-21-2023, 04:21 AM
Last Post: deanhystad
  Failing to print sorted files tester_V 4 1,188 Nov-12-2022, 06:49 PM
Last Post: tester_V
  delete all files which contains word sql_Table1 mg24 2 824 Sep-15-2022, 10:05 PM
Last Post: mg24
  [GoogleTrans] How can i print my translation word ?... JamieVanCadsand 7 11,629 Aug-29-2021, 12:01 PM
Last Post: Melcu54
Question Problem: Check if a list contains a word and then continue with the next word Mangono 2 2,455 Aug-12-2021, 04:25 PM
Last Post: palladium
  Moving specific files then unzipping/decompressing christophereccles 2 2,326 Apr-24-2021, 04:25 AM
Last Post: ndc85430
  How to make a telegram bot respond to the specific word in a sentence? Metodolog 2 6,274 Dec-22-2020, 07:30 AM
Last Post: martabassof
  Searching for specific word in text files. JellyCreeper6 1 1,698 Nov-03-2020, 01:52 PM
Last Post: DeaD_EyE
  Find specific subdir, open files and find specific lines that are missing from a file tester_V 8 3,491 Aug-25-2020, 01:52 AM
Last Post: tester_V

Forum Jump:

User Panel Messages

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