fnmatch

Python, how to implement something like .gitignore behavior

末鹿安然 提交于 2019-12-29 06:46:49
问题 I need to list all files in the current directory (.) (including all sub directories), and exclude some files as how .gitignore works (http://git-scm.com/docs/gitignore) With fnmatch (https://docs.python.org/2/library/fnmatch.html) I will be able to "filter" files using a pattern ignore_files = ['*.jpg', 'foo/', 'bar/hello*'] matches = [] for root, dirnames, filenames in os.walk('.'): for filename in fnmatch.filter(filenames, '*'): matches.append(os.path.join(root, filename)) how can I

get also element that don't match fnmatch

寵の児 提交于 2019-12-07 13:51:27
问题 I'm using a recursive glob to find and copy files from a drive to another def recursive_glob(treeroot, pattern): results = [] for base, dirs, files in os.walk(treeroot): goodfiles = fnmatch.filter(files, pattern) results.extend(os.path.join(base, f) for f in goodfiles) return results Works fine. But I also want to have access to the elements that don't match the filter. Can someone offer some help? I could build a regex within the loop, but there must be a simpler solution, right? Thanks in

pathinfo vs fnmatch

。_饼干妹妹 提交于 2019-12-01 17:01:03
问题 There was a small debate regarding the speed of fnmatch over pathinfo here : how to check if file is php? I wasn't totally convinced so decided to benchmark the two functions. Using dynamic and static paths showed that pathinfo was faster. Is my benchmarking logic and conclusion valid? EDIT : Using mac php from cmd PHP 5.3.0 (cli) (built: Jul 20 2009 13:56:33) Copyright (c) 1997-2009 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2009 Zend Technologies dynamic path pathinfo 3

pathinfo vs fnmatch

ぐ巨炮叔叔 提交于 2019-12-01 16:31:25
There was a small debate regarding the speed of fnmatch over pathinfo here : how to check if file is php? I wasn't totally convinced so decided to benchmark the two functions. Using dynamic and static paths showed that pathinfo was faster. Is my benchmarking logic and conclusion valid? EDIT : Using mac php from cmd PHP 5.3.0 (cli) (built: Jul 20 2009 13:56:33) Copyright (c) 1997-2009 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2009 Zend Technologies dynamic path pathinfo 3.2973630428314 fnmatch 3.4520659446716 x1.05 static path pathinfo 0.86487698554993 fnmatch 1.0420439243317 x1.2

Python, how to implement something like .gitignore behavior

不羁的心 提交于 2019-11-29 06:56:31
I need to list all files in the current directory (.) (including all sub directories), and exclude some files as how .gitignore works ( http://git-scm.com/docs/gitignore ) With fnmatch ( https://docs.python.org/2/library/fnmatch.html ) I will be able to "filter" files using a pattern ignore_files = ['*.jpg', 'foo/', 'bar/hello*'] matches = [] for root, dirnames, filenames in os.walk('.'): for filename in fnmatch.filter(filenames, '*'): matches.append(os.path.join(root, filename)) how can I "filter" and get all files which doesn't match with one or more element of my "ignore_files"? Thanks! You

How to count the number of files in a directory using Python

删除回忆录丶 提交于 2019-11-27 10:10:54
I need to count the number of files in a directory using Python. I guess the easiest way is len(glob.glob('*')) , but that also counts the directory itself as a file. Is there any way to count only the files in a directory? Daniel Stutzbach os.listdir() will be slightly more efficient than using glob.glob . To test if a filename is an ordinary file (and not a directory or other entity), use os.path.isfile() : import os, os.path # simple version for working with CWD print len([name for name in os.listdir('.') if os.path.isfile(name)]) # path joining version for other paths DIR = '/tmp' print

How to count the number of files in a directory using Python

…衆ロ難τιáo~ 提交于 2019-11-26 23:48:10
问题 I need to count the number of files in a directory using Python. I guess the easiest way is len(glob.glob('*')) , but that also counts the directory itself as a file. Is there any way to count only the files in a directory? 回答1: os.listdir() will be slightly more efficient than using glob.glob . To test if a filename is an ordinary file (and not a directory or other entity), use os.path.isfile() : import os, os.path # simple version for working with CWD print len([name for name in os.listdir(

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

不想你离开。 提交于 2019-11-26 15:48:57
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 the same command to access subfolders as well? In Python 3.5 and newer use the new recursive **/ functionality: configfiles = glob.glob('C:/Users/sam/Desktop/file1/**/*.txt', recursive=True) When recursive is set, ** followed by a path separator matches 0 or more subdirectories. In earlier Python versions, glob.glob() cannot

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

时间秒杀一切 提交于 2019-11-26 06:00:10
问题 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 the same command to access subfolders as well? 回答1: In Python 3.5 and newer use the new recursive **/ functionality: configfiles = glob.glob('C:/Users/sam/Desktop/file1/**/*.txt', recursive=True) When recursive is set, **

How to use glob() to find files recursively?

霸气de小男生 提交于 2019-11-25 21:56:55
问题 This is what I have: glob(os.path.join(\'src\',\'*.c\')) but I want to search the subfolders of src. Something like this would work: glob(os.path.join(\'src\',\'*.c\')) glob(os.path.join(\'src\',\'*\',\'*.c\')) glob(os.path.join(\'src\',\'*\',\'*\',\'*.c\')) glob(os.path.join(\'src\',\'*\',\'*\',\'*\',\'*.c\')) But this is obviously limited and clunky. 回答1: Python 3.5+ Since you're on a new python, you should use pathlib.Path.rglob from the the pathlib module. from pathlib import Path for