Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find files in a directory with a partial string match

Tags:

I have a directory which contains the following files:

apple1.json.gz
apple2.json.gz
banana1.json.gz
melon1.json.gz
melon2.json.gz

I wish to find all of the apple, banana and melon file types.

From this SO answer I know that I can find by file type by:

import glob, os
os.chdir("/mydir")
for file in glob.glob("*.json.gz"):
    print(file)

However, in my case I can't match by file name or file type. Rather it is a partial file name match (all apple's and so on)

In this SO question, this solution was proposed:

[in] for file in glob.glob('/path/apple*.json.gz'):
    print file

However, this returns zero

[out]
     0
like image 255
LearningSlowly Avatar asked May 24 '16 17:05

LearningSlowly


People also ask

How do we search for all the files whose name start from test in the home directory?

Finding Files with bat Anywhere To search your whole computer, use / . To search your home directory, use ~ , or the full name of your home directory. (The shell expands ~ to your home directory's fully qualified path.)

How will you find a file name containing fee in the name in Linux?

Your answer You can use “grep” command to search string in files. Alternatively, You can also also use the "find " command to display files with specific string. Hope this answer help you. To learn more about Linux, enroll in Linux administration course online today.

How do I search for a file in a directory in Python?

Python can search for file names in a specified path of the OS. This can be done using the module os with the walk() functions. This will take a specific path as input and generate a 3-tuple involving dirpath, dirnames, and filenames. In the below example we are searching for a file named smpl.


2 Answers

Having the files in /mydir as follows

mydir
├── apple1.json.gz
├── apple2.json.gz
├── banana1.json.gz
├── melon1.json.gz
└── melon2.json.gz

you could either do

import glob
import os

os.chdir('/mydir')
for file in glob.glob('apple*.json.gz'):
    print file

or

import glob

for file in glob.glob('/mydir/apple*.json.gz'):
    print file

Changing directories will not effect glob.glob('/absolute/path').

like image 113
Simon Fromme Avatar answered Sep 20 '22 01:09

Simon Fromme


Double List Comprehension Method

I was looking for a similar tool and developed a double list comprehension method that should work well for your case (I tested it for my case) ...

import os

def get_file_names_with_strings(str_list):
    full_list = os.listdir("path_to_your_dir")
    final_list = [nm for ps in str_list for nm in full_list if ps in nm]

    return final_list


print(get_file_names_with_strings(['apple', 'banana', 'melon']))
like image 21
Thom Ives Avatar answered Sep 18 '22 01:09

Thom Ives