glob

Inverse glob – reverse engineer a wildcard string from file names

Inverse glob – reverse engineer a wildcard string from file names Question: I want to generate a wildcard string from a pair of file names. Kind of an inverse-glob. Example: file1 = ‘some foo file.txt’ file2 = ‘some bar file.txt’ assert ‘some * file.txt’ == inverse_glob(file1, file2) Use difflib perhaps? Has this been solved already? …

Total answers: 2

Loop over results from Path.glob() (Pathlib)

Loop over results from Path.glob() (Pathlib) Question: I’m struggling with the result of the Path.glob() method of the Pathlib module in Python 3.6. from pathlib import Path dir = Path.cwd() files = dir.glob(‘*.txt’) print(list(files)) >> [WindowsPath(‘C:/whatever/file1.txt’), WindowsPath(‘C:/whatever/file2.txt’)] for file in files: print(file) print(‘Check.’) >> Evidently, glob found files, but the for-loop is not executed. How …

Total answers: 1

Python Pandas add Filename Column CSV

Python Pandas add Filename Column CSV Question: My python code works correctly in the below example. My code combines a directory of CSV files and matches the headers. However, I want to take it a step further – how do I add a column that appends the filename of the CSV that was used? import …

Total answers: 3

Python glob but against a list of strings rather than the filesystem

Python glob but against a list of strings rather than the filesystem Question: I want to be able to match a pattern in glob format to a list of strings, rather than to actual files in the filesystem. Is there any way to do this, or convert a glob pattern easily to a regex? Asked …

Total answers: 10

Glob search files in date order?

Glob search files in date order? Question: I have this line of code in my python script. It searches all the files in in a particular directory for * cycle *.log. for searchedfile in glob.glob(“*cycle*.log”): This works perfectly, however when I run my script to a network location it does not search them in order …

Total answers: 6

Brace expansion in python glob

Brace expansion in python glob Question: I have python 2.7 and am trying to issue: glob(‘{faint,bright*}/{science,calib}/chip?/’) I obtain no matches, however from the shell echo {faint,bright*}/{science,calib}/chip? gives: faint/science/chip1 faint/science/chip2 faint/calib/chip1 faint/calib/chip2 bright1/science/chip1 bright1/science/chip2 bright1w/science/chip1 bright1w/science/chip2 bright2/science/chip1 bright2/science/chip2 bright2w/science/chip1 bright2w/science/chip2 bright1/calib/chip1 bright1/calib/chip2 bright1w/calib/chip1 bright1w/calib/chip2 bright2/calib/chip1 bright2/calib/chip2 bright2w/calib/chip1 bright2w/calib/chip2 What is wrong with my expression? Asked By: …

Total answers: 6

glob exclude pattern

glob exclude pattern Question: I have a directory with a bunch of files inside: eee2314, asd3442 … and eph. I want to exclude all files that start with eph with the glob function. How can I do it? Asked By: Anastasios Andronidis || Source Answers: You can’t exclude patterns with the glob function, globs only …

Total answers: 12

How can I search sub-folders using glob.glob module?

How can I search sub-folders using glob.glob module? Question: I want to open a series of subfolders in a folder and find some text files and print some lines of the text files. I am using this: configfiles = glob.glob(‘C:/Users/sam/Desktop/file1/*.txt’) But this cannot access the subfolders as well. Does anyone know how I can use …

Total answers: 13

Regular expression usage in glob.glob?

Regular expression usage in glob.glob? Question: import glob list = glob.glob(r’*abc*.txt’) + glob.glob(r’*123*.txt’) + glob.glob(r’*a1b*.txt’) for i in list: print i This code works to list files in the current folder which have ‘abc’, ‘123’ or ‘a1b’ in their names. How would I use one glob to perform this function? Asked By: user1561868 || Source …

Total answers: 4