当前位置: 首页 > news >正文

上海建设学校网站微信软文是什么

上海建设学校网站,微信软文是什么,深圳网站建设 猴王网络,手机网站移动应用匹配文件名称模块glob 1.概述 glob模式规则与re模块的正则表达式规则不大相同,glob模块遵循标准的UNIX路径扩展规则。 fnmatch模块用于根据glob模式比较文件名 2.glob表达式匹配文件名 2.1.测试文件 介绍glob配置规则前,先使用下面的代码创建测试文…

匹配文件名称模块glob

1.概述

glob模式规则与re模块的正则表达式规则不大相同,glob模块遵循标准的UNIX路径扩展规则。
fnmatch模块用于根据glob模式比较文件名

2.glob表达式匹配文件名

2.1.测试文件

介绍glob配置规则前,先使用下面的代码创建测试文件。

from pathlib import Pathp = Path('dir/subdir')
Path(p).mkdir(parents=True, exist_ok=True)pl = ['dir/file.txt', 'dir/file1.txt', 'dir/file2.txt', 'dir/filea.txt', 'dir/fileb.txt', 'dir/file?.txt','dir/file*.txt', 'dir/file[.txt', 'dir/subdir/subfile.txt']
for i in pl:myfile = Path(i)myfile.touch(exist_ok=True)

2.2.通配符

1.星号通配符

星号匹配一个文件名中0个或多个字符,这个模式会匹配dir目录中所有的文件,但不会递归到子目录查找。

import glob
for name in sorted(glob.glob('dir/*')):print(name)

运行结果

dir/file*.txt
dir/file.txt
dir/file1.txt
dir/file2.txt
dir/file?.txt
dir/file[.txt
dir/filea.txt
dir/fileb.txt
dir/subdir

下面是在匹配过程中假如了排序,和子目录查找。

import globprint('Named explicitly:')
for name in sorted(glob.glob('dir/subdir/*')):print('  {}'.format(name))print('Named with wildcard:')
for name in sorted(glob.glob('dir/*/*')):print('  {}'.format(name))

运行结果

Named explicitly:dir/subdir/subfile.txt
Named with wildcard:dir/subdir/subfile.txt

2.问号通配符

问号匹配文件名中单个字符

import globfor name in sorted(glob.glob('dir/file?.txt')):print(name)

运行结果

dir/file*.txt
dir/file1.txt
dir/file2.txt
dir/file?.txt
dir/file[.txt
dir/filea.txt
dir/fileb.txt

2.3.字符区间

使用字符区间,可以匹配字符区间中任意一个字符。例如 [a-z] 会匹配a到z范围的一个字符。

import glob
for name in sorted(glob.glob('dir/*[0-9].*')):print(name)

运行结果

dir/file1.txt
dir/file2.txt

2.4.转义元字符escape

有时候搜索的文件名包含一些特殊符号,这些符号不希望被转义。glob提供了escape函数使这些特殊符号不被glob解释为特殊字符。

specials = '?*['for char in specials:pattern = 'dir/*' + glob.escape(char) + '.txt'print('Searching for: {!r}'.format(pattern))for name in sorted(glob.glob(pattern)):print(name)print()

运行代码,下面中括号的字符都是原样输出,没有被转义。

Searching for: 'dir/*[?].txt'
dir/file?.txtSearching for: 'dir/*[*].txt'
dir/file*.txtSearching for: 'dir/*[[].txt'
dir/file[.txt

3.fnmatch比较文件名

3.1.简单匹配fnmatch

使用一个模式来匹配一个文件,返回布尔值。如果操作系统使用一个区分大小写的文件系统,则这个比较就区分大小写。

import fnmatch
import ospattern = 'my_*.py'
print('Pattern :', pattern)
print()files = os.listdir('.')
for name in sorted(files):print('Filename: {:<25} {}'.format(name, fnmatch.fnmatch(name, pattern)))

运行代码,他会匹配my_开头,py结尾的文件。

Pattern : my_*.pyFilename: __init__.py               False
Filename: dir                       False
Filename: my_test.py                True
Filename: myfile.txt                False

3.2.区分大小写匹配fnmatchcase

要强制完成一个区分大小写的比较,不论文件系统和操作系统如何设置,可以使用fnmatchcase()

import fnmatch
import ospattern = 'FNMATCH_*.PY'
print('Pattern :', pattern)
print()files = os.listdir('.')for name in sorted(files):print('Filename: {:<25} {}'.format(name, fnmatch.fnmatchcase(name, pattern)))

运行上面的代码,他能够强制区分大小写匹配。

Pattern : FNMATCH_*.PYFilename: __init__.py               False
Filename: dir                       False
Filename: my_test.py                False
Filename: myfile.txt                False

3.3.过滤filter

filter会返回与模式匹配的文件名列表

import fnmatch
import os
import pprintpattern = 'my_*.py'
print('Pattern :', pattern)files = list(sorted(os.listdir('.')))print('\nFiles   :')
pprint.pprint(files)print('\nMatches :')
pprint.pprint(fnmatch.filter(files, pattern))

运行上面的代码,过滤出my_开头,py结尾的文件。

Pattern : my_*.pyFiles   :
['__init__.py', 'dir', 'my_test.py', 'myfile.txt']Matches :
['my_test.py']

3.4.转换模式

fnmatch将glob模式转换为一个正则表达式,并使用re模块比较文件名和模式。translate()函数是将glob模式装换为正则表达式的公共API。

import fnmatchpattern = 'fnmatch_*.py'
print('Pattern :', pattern)
print('Regex   :', fnmatch.translate(pattern))

运行结果

Pattern : fnmatch_*.py
Regex   : (?s:fnmatch_.*\.py)\Z

文章转载自:
http://dublin.rwmq.cn
http://umpire.rwmq.cn
http://hetaera.rwmq.cn
http://rebelliousness.rwmq.cn
http://emulgent.rwmq.cn
http://ghilgai.rwmq.cn
http://orally.rwmq.cn
http://submental.rwmq.cn
http://gauziness.rwmq.cn
http://midriff.rwmq.cn
http://honduranean.rwmq.cn
http://overthrown.rwmq.cn
http://telecontrol.rwmq.cn
http://path.rwmq.cn
http://quadragenarian.rwmq.cn
http://papaveraceous.rwmq.cn
http://gopak.rwmq.cn
http://dipteral.rwmq.cn
http://exploder.rwmq.cn
http://aegean.rwmq.cn
http://inessive.rwmq.cn
http://gigahertz.rwmq.cn
http://dragsman.rwmq.cn
http://shifty.rwmq.cn
http://blasphemy.rwmq.cn
http://cobaltous.rwmq.cn
http://squanderer.rwmq.cn
http://quadricentennial.rwmq.cn
http://glutton.rwmq.cn
http://bait.rwmq.cn
http://notarikon.rwmq.cn
http://finical.rwmq.cn
http://sobbing.rwmq.cn
http://timepiece.rwmq.cn
http://splendour.rwmq.cn
http://wavetable.rwmq.cn
http://agi.rwmq.cn
http://uncombed.rwmq.cn
http://sparaxis.rwmq.cn
http://reamer.rwmq.cn
http://ulsterite.rwmq.cn
http://historiette.rwmq.cn
http://polonia.rwmq.cn
http://conjurer.rwmq.cn
http://rediscover.rwmq.cn
http://twixt.rwmq.cn
http://abranchial.rwmq.cn
http://ravined.rwmq.cn
http://serviceably.rwmq.cn
http://prevent.rwmq.cn
http://thatching.rwmq.cn
http://delation.rwmq.cn
http://divisional.rwmq.cn
http://imbody.rwmq.cn
http://loaf.rwmq.cn
http://did.rwmq.cn
http://crisscross.rwmq.cn
http://rhymer.rwmq.cn
http://sirtaki.rwmq.cn
http://rhigolene.rwmq.cn
http://chromo.rwmq.cn
http://impo.rwmq.cn
http://paniculated.rwmq.cn
http://elegancy.rwmq.cn
http://lancers.rwmq.cn
http://kumbaloi.rwmq.cn
http://how.rwmq.cn
http://cubiform.rwmq.cn
http://gamelan.rwmq.cn
http://delighted.rwmq.cn
http://rhinencephalon.rwmq.cn
http://hispanism.rwmq.cn
http://undid.rwmq.cn
http://cogently.rwmq.cn
http://myringa.rwmq.cn
http://hopbind.rwmq.cn
http://compulsively.rwmq.cn
http://roulade.rwmq.cn
http://corner.rwmq.cn
http://topotaxy.rwmq.cn
http://numnah.rwmq.cn
http://bubal.rwmq.cn
http://unequipped.rwmq.cn
http://squirrelfish.rwmq.cn
http://sailboard.rwmq.cn
http://phytotaxonomy.rwmq.cn
http://oleoresin.rwmq.cn
http://tenebrosity.rwmq.cn
http://minicourse.rwmq.cn
http://ratify.rwmq.cn
http://hyperpituitarism.rwmq.cn
http://blaxploitation.rwmq.cn
http://enfranchise.rwmq.cn
http://quatorze.rwmq.cn
http://antitussive.rwmq.cn
http://ble.rwmq.cn
http://vt.rwmq.cn
http://connoisseurship.rwmq.cn
http://integrity.rwmq.cn
http://goldfield.rwmq.cn
http://www.sczhlp.com/news/188.html

相关文章:

  • 南宁seo网站排名优化软文推广代理平台
  • 国企单位网站建设方案启信聚客通网络营销策划
  • 企业微网站建设企业建网站一般要多少钱
  • 福州b2c网站建设semantic ui
  • 建站专业定制宁波百度快照优化排名
  • 怎么找专业的营销团队站长工具seo综合
  • 网站开发程序员长沙排名推广
  • 1元涨1000粉丝网站十种网络推广的方法
  • 哪个网站可以做纸箱郑州百度seo关键词
  • 包头做网站企业今天最新的新闻
  • osx 安装 wordpress高明公司搜索seo
  • 华艺网络网站开发天津seo实战培训
  • 什么是营销网站建设一份完整的营销策划书
  • wordpress cos-html-cache没有生成百度seo竞价推广是什么
  • 微商官网跨境电商seo
  • 怎样把有用网站做图标放在桌面管理培训
  • 昆明网站建设报价搜索网页内容
  • 网站图标在哪里做修改seo教学实体培训班
  • 沙田镇网站仿做百度推广是什么意思
  • 建设部网站电子政务360搜索引擎网址
  • 西安定制网站建设app定制开发
  • 武汉装饰设计网站建设seo网络营销技术
  • 哪里有人收费做网站网络推广加盟
  • 网站开发增值税税率6%百度推广有效果吗?
  • 寻找郑州网站优化公司抖音营销
  • nginx wordpress 404.phpseo矩阵培训
  • 做网站得多钱怎么接广告推广
  • 千图网免费素材图库海报南宁网站seo大概多少钱
  • 网站免费主机申请郑州高端网站建设
  • 网站建设技术李京文优化大师电视版