[英]File name matching with fnmatch
I have a directory with files with the format: LnLnnnnLnnn.txt
我有一个文件目录,格式为:
LnLnnnnLnnn.txt
where L = letters and n = numbers. 其中L =字母,n =数字。 Eg:
p2c0789c001.txt
例如:
p2c0789c001.txt
I would like to separate these files based on whether the second number (ie 0789) is within a certain sequence of numbers (eg 0001 to 0146). 我想根据第二个数字(即0789)是否在特定的数字序列(例如0001到0146)内来分离这些文件。
Is there an easy way to do this with fnmatch? 使用fnmatch有一个简单的方法吗? Or should I be using regex?
或者我应该使用正则表达式?
This is the code I have so far: 这是我到目前为止的代码:
out_files = []
for root, dirs, filenames in os.walk('.'):
for filename in fnmatch.filter(filenames, '???[0-9][0-9][0-9][0-9]????*.txt'):
out_files.append(os.path.join(root, filename))
You can't do it easily inside fnmatch.filter()
, but you could do it yourself: 你不能在
fnmatch.filter()
轻松fnmatch.filter()
,但你可以自己做:
out_files = []
for root, dirs, filenames in os.walk('.'):
for filename in fnmatch.filter(filenames, '???[0-9][0-9][0-9][0-9]????*.txt'):
if(1 <= int(filename[3:7]) <= 146):
out_files.append(os.path.join(root, filename))
Or, for the list-comprehension fans: 或者,对于列表理解粉丝:
import os
import fnmatch
out_files = [os.path.join(root, filename)
for root, dirs, filenames in os.walk('.')
for filename in fnmatch.filter(filenames,
'???[0-9][0-9][0-9][0-9]????*.txt')
if 1 <= int(filename[3:7]) <= 146]
EDIT : Whoops, forgot an extra for loop. 编辑 :哎呀,忘了一个额外的循环。 Also, see if this has better performance.
另外,看看这是否有更好的性能。
EDIT2 : Just in case the first letter is a c
, checks the second to last element, which based on the criteria for both alternatives is guaranteed to exist. EDIT2 :如果第一个字母是
c
,则检查倒数第二个元素,该元素基于两个备选方案的标准,保证存在。
out_files = []
for root, dirs, filenames in os.walk('.'):
for filename in filesnames:
try:
if 1 <= int(filename.split('c')[-2]) <= 146:
out_files.append(...)
except IndexError:
continue
Alternatively, using a generator: 或者,使用发电机:
out_files = []
for root, dirs, filenames in os.walk('.'):
for filename in (name for name in filenames if 'c' in name):
if 1 <= int(filename.split('c')[-2]) <= 146:
out_files.append(...)
In case there are other c's
at the start of the string or the string length before the numbers changes: 如果字符串开头有其他
c's
或数字更改前的字符串长度:
if 1 <= int(re.findall(r"c([0-9]+)c", s)[0]) <= 487
: if 1 <= int(re.findall(r"c([0-9]+)c", s)[0]) <= 487
:
Or if there are always four digits: 或者,如果总有四位数字:
if 1 <= int(re.findall(r"c(\d{4})c", s)[0]) <= 487:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.