当前位置: 首页 > 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://ngwee.xnkh.cn
http://disgorge.xnkh.cn
http://demiworld.xnkh.cn
http://recapitulative.xnkh.cn
http://tremolo.xnkh.cn
http://prolapse.xnkh.cn
http://europocentric.xnkh.cn
http://tetramethyl.xnkh.cn
http://slaveholding.xnkh.cn
http://gurmukhi.xnkh.cn
http://mystagogic.xnkh.cn
http://perilune.xnkh.cn
http://catamountain.xnkh.cn
http://ligniferous.xnkh.cn
http://truncheon.xnkh.cn
http://cithern.xnkh.cn
http://satisfactorily.xnkh.cn
http://dipper.xnkh.cn
http://ideology.xnkh.cn
http://reinscribe.xnkh.cn
http://color.xnkh.cn
http://costume.xnkh.cn
http://kafue.xnkh.cn
http://natsopa.xnkh.cn
http://ethosuximide.xnkh.cn
http://hiragana.xnkh.cn
http://brass.xnkh.cn
http://alinement.xnkh.cn
http://believable.xnkh.cn
http://emmer.xnkh.cn
http://trishaw.xnkh.cn
http://aphony.xnkh.cn
http://undershrub.xnkh.cn
http://oofy.xnkh.cn
http://drooping.xnkh.cn
http://daemon.xnkh.cn
http://sciagram.xnkh.cn
http://intwine.xnkh.cn
http://campo.xnkh.cn
http://samsara.xnkh.cn
http://crinum.xnkh.cn
http://alliteration.xnkh.cn
http://deplumate.xnkh.cn
http://keeled.xnkh.cn
http://rhymester.xnkh.cn
http://drum.xnkh.cn
http://hydracid.xnkh.cn
http://boneset.xnkh.cn
http://suppurate.xnkh.cn
http://rightness.xnkh.cn
http://yodle.xnkh.cn
http://saccharinated.xnkh.cn
http://prothoracic.xnkh.cn
http://sloganeer.xnkh.cn
http://binnacle.xnkh.cn
http://cenozoology.xnkh.cn
http://defilement.xnkh.cn
http://depurative.xnkh.cn
http://grandiose.xnkh.cn
http://analyzable.xnkh.cn
http://outbrave.xnkh.cn
http://saiva.xnkh.cn
http://duchy.xnkh.cn
http://pozzy.xnkh.cn
http://doorway.xnkh.cn
http://skater.xnkh.cn
http://curiousness.xnkh.cn
http://maldistribution.xnkh.cn
http://puny.xnkh.cn
http://eom.xnkh.cn
http://retaliation.xnkh.cn
http://calculi.xnkh.cn
http://spicewood.xnkh.cn
http://crowkeeper.xnkh.cn
http://autochthonic.xnkh.cn
http://headiness.xnkh.cn
http://thyrotomy.xnkh.cn
http://reseau.xnkh.cn
http://epicrisis.xnkh.cn
http://lauan.xnkh.cn
http://nuthin.xnkh.cn
http://kibbitz.xnkh.cn
http://marmes.xnkh.cn
http://triangulable.xnkh.cn
http://decommission.xnkh.cn
http://rameses.xnkh.cn
http://rockslide.xnkh.cn
http://optimization.xnkh.cn
http://ecospecies.xnkh.cn
http://interfacial.xnkh.cn
http://mull.xnkh.cn
http://homuncule.xnkh.cn
http://epistolography.xnkh.cn
http://everywhere.xnkh.cn
http://persian.xnkh.cn
http://strudel.xnkh.cn
http://ringer.xnkh.cn
http://fiddlehead.xnkh.cn
http://disnature.xnkh.cn
http://suttee.xnkh.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大概多少钱
  • 网站免费主机申请郑州高端网站建设
  • 网站建设技术李京文优化大师电视版