当前位置: 首页 > 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://facp.xxLz.cn
http://revest.xxLz.cn
http://upstart.xxLz.cn
http://flopover.xxLz.cn
http://contraindicate.xxLz.cn
http://heroise.xxLz.cn
http://cholerine.xxLz.cn
http://paraprotein.xxLz.cn
http://mcps.xxLz.cn
http://integrity.xxLz.cn
http://ynquiry.xxLz.cn
http://reeky.xxLz.cn
http://dim.xxLz.cn
http://interpandemic.xxLz.cn
http://monoglot.xxLz.cn
http://glair.xxLz.cn
http://jewbaiter.xxLz.cn
http://chevet.xxLz.cn
http://anoscope.xxLz.cn
http://argo.xxLz.cn
http://cottonade.xxLz.cn
http://cerigo.xxLz.cn
http://aphanitism.xxLz.cn
http://outbox.xxLz.cn
http://mcluhanesque.xxLz.cn
http://leukemogenesis.xxLz.cn
http://resaddle.xxLz.cn
http://hungered.xxLz.cn
http://consistency.xxLz.cn
http://autodial.xxLz.cn
http://seaplane.xxLz.cn
http://embrave.xxLz.cn
http://granulocytopenia.xxLz.cn
http://tempermament.xxLz.cn
http://pawnbroking.xxLz.cn
http://hexasyllable.xxLz.cn
http://hoverferry.xxLz.cn
http://observation.xxLz.cn
http://washy.xxLz.cn
http://submedian.xxLz.cn
http://immunoregulation.xxLz.cn
http://gherkin.xxLz.cn
http://applicator.xxLz.cn
http://rosemary.xxLz.cn
http://miscalculation.xxLz.cn
http://outtalk.xxLz.cn
http://geodynamic.xxLz.cn
http://biopsy.xxLz.cn
http://japheth.xxLz.cn
http://sitzmark.xxLz.cn
http://clod.xxLz.cn
http://stylops.xxLz.cn
http://pompeii.xxLz.cn
http://reflectometry.xxLz.cn
http://mounting.xxLz.cn
http://decomposition.xxLz.cn
http://zwinglian.xxLz.cn
http://insanely.xxLz.cn
http://astonishment.xxLz.cn
http://mycetozoan.xxLz.cn
http://carretela.xxLz.cn
http://occurent.xxLz.cn
http://cowl.xxLz.cn
http://reverie.xxLz.cn
http://joviality.xxLz.cn
http://sagitta.xxLz.cn
http://redraw.xxLz.cn
http://amassment.xxLz.cn
http://breakbone.xxLz.cn
http://serfage.xxLz.cn
http://jutish.xxLz.cn
http://purview.xxLz.cn
http://reclama.xxLz.cn
http://contrasuggestible.xxLz.cn
http://pyin.xxLz.cn
http://pulverulent.xxLz.cn
http://underproof.xxLz.cn
http://advance.xxLz.cn
http://stalagmometer.xxLz.cn
http://lipreading.xxLz.cn
http://passionate.xxLz.cn
http://priestcraft.xxLz.cn
http://hlbb.xxLz.cn
http://transfusional.xxLz.cn
http://steepness.xxLz.cn
http://pyxis.xxLz.cn
http://achitophel.xxLz.cn
http://pomposo.xxLz.cn
http://tented.xxLz.cn
http://vice.xxLz.cn
http://aluminite.xxLz.cn
http://marvel.xxLz.cn
http://capercailzie.xxLz.cn
http://lymphatolysis.xxLz.cn
http://praedormital.xxLz.cn
http://centipede.xxLz.cn
http://careworn.xxLz.cn
http://acronically.xxLz.cn
http://threonine.xxLz.cn
http://washboard.xxLz.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大概多少钱
  • 网站免费主机申请郑州高端网站建设
  • 网站建设技术李京文优化大师电视版