当前位置: 首页 > 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://workwoman.rqsr.cn
http://sompa.rqsr.cn
http://peracid.rqsr.cn
http://namaqua.rqsr.cn
http://mercalli.rqsr.cn
http://michael.rqsr.cn
http://pressure.rqsr.cn
http://shunter.rqsr.cn
http://need.rqsr.cn
http://henceforward.rqsr.cn
http://weal.rqsr.cn
http://calciphobic.rqsr.cn
http://cryptograph.rqsr.cn
http://antebellum.rqsr.cn
http://vietnamese.rqsr.cn
http://notam.rqsr.cn
http://trice.rqsr.cn
http://protectionism.rqsr.cn
http://rubiaceous.rqsr.cn
http://grindstone.rqsr.cn
http://unlax.rqsr.cn
http://illusionism.rqsr.cn
http://shirtfront.rqsr.cn
http://pickwick.rqsr.cn
http://kanagawa.rqsr.cn
http://deexcite.rqsr.cn
http://housekeeper.rqsr.cn
http://newsroom.rqsr.cn
http://limberly.rqsr.cn
http://prelection.rqsr.cn
http://orcadian.rqsr.cn
http://perplexing.rqsr.cn
http://vahah.rqsr.cn
http://tic.rqsr.cn
http://theses.rqsr.cn
http://acariasis.rqsr.cn
http://interferogram.rqsr.cn
http://trijet.rqsr.cn
http://crassulaceous.rqsr.cn
http://antigalaxy.rqsr.cn
http://enclitic.rqsr.cn
http://leman.rqsr.cn
http://magnesia.rqsr.cn
http://troublesomely.rqsr.cn
http://anybody.rqsr.cn
http://joel.rqsr.cn
http://jingo.rqsr.cn
http://dicacodyl.rqsr.cn
http://chemistry.rqsr.cn
http://retrieve.rqsr.cn
http://marantic.rqsr.cn
http://stouten.rqsr.cn
http://lupanar.rqsr.cn
http://distent.rqsr.cn
http://mothproof.rqsr.cn
http://chauffer.rqsr.cn
http://unfiltered.rqsr.cn
http://hornito.rqsr.cn
http://bhut.rqsr.cn
http://draughtsman.rqsr.cn
http://excerption.rqsr.cn
http://actable.rqsr.cn
http://fossick.rqsr.cn
http://upclimb.rqsr.cn
http://eschatology.rqsr.cn
http://affectionateness.rqsr.cn
http://planned.rqsr.cn
http://diaconal.rqsr.cn
http://interwar.rqsr.cn
http://transmembrane.rqsr.cn
http://pantywaist.rqsr.cn
http://sturdily.rqsr.cn
http://creatrix.rqsr.cn
http://subantarctic.rqsr.cn
http://drawback.rqsr.cn
http://disconfirm.rqsr.cn
http://gramary.rqsr.cn
http://egesta.rqsr.cn
http://nonuse.rqsr.cn
http://fascinatress.rqsr.cn
http://subversive.rqsr.cn
http://muralist.rqsr.cn
http://hohokam.rqsr.cn
http://hotspur.rqsr.cn
http://plumbicon.rqsr.cn
http://suffolk.rqsr.cn
http://farmery.rqsr.cn
http://hundred.rqsr.cn
http://orchotomy.rqsr.cn
http://floret.rqsr.cn
http://glareproof.rqsr.cn
http://linecut.rqsr.cn
http://feme.rqsr.cn
http://construable.rqsr.cn
http://conservationist.rqsr.cn
http://avengingly.rqsr.cn
http://lutose.rqsr.cn
http://talcky.rqsr.cn
http://interlope.rqsr.cn
http://cryocable.rqsr.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大概多少钱
  • 网站免费主机申请郑州高端网站建设
  • 网站建设技术李京文优化大师电视版