首页 > 编程知识 正文

python crypto模块详解,glm模型原理

时间:2023-05-05 16:00:37 阅读:155838 作者:1674

欢迎大家关注。 你的关注是我继续更博的最大动力

原创文章、转载通告、盗版必究

python的标准库模块glob使用教程主要是使用glob.glob函数和使用glob.iglob函数

文章目录:1 glob模块介绍2 glob模块的具体使用方法2 glob模块如何使用属性2.2glob.glob(Pathname,*, recursive=False (使用函数2.2.1函数glob.glob ) (定义:2.2.2 glob.glob ) ) )函数的参数和返回值2.2.3 glob.glob ) ) ob ) )函数定义2.3.2 glob.iglob ) )函数的参数2.3.3 glob.iglob ) )函数用法示例2.4其他通配符` *?[] `实例

1 glob模块简介

glob是python的标准库模块,安装python后即可使用。 glob模块主要用于搜索目录和文件,*,[]这三种通配符用于匹配路径中的文件。

*表示0个以上的字符吗?表示1个字符[]。 匹配指定范围内的字符,例如[0-9]。 匹配数字Unix样式的路径名模式扩展

glob模块的英文文档: https://docs.python.org/3/library/glob.html2glob模块的具体使用方法2.1glob模块有哪些方法属性dir [ ' _ _ aaaaaaate _rlistdir '、escape '、fnmatch '、glob '、glob0'、glob1'、has_magic '、iglob '、magic_checcheck

2.2使用glob.glob (pathname,*,recursive=False ) )函数2.2.1函数glob.glob )的定义: defglob ) pathname,*, recursive=False ) 3360 ' ' returnalistofpathsmatchingapathnamepattern.thepatternmaycontainsimpleshell-stylewildcardsalafard filenamesstartingwithadotarespecialcasesthatarenotmatchedby ' * ' and '?' patterns. If recursive is true,the pattern ' * ' willmatchanyfilesandzeroormoredirectoriesandsubdirectories.' ' ' return list recursive=recursive (defi glob ) pathname,*, recursive=False ) 3360 ' ' returnaniteratorwhichyieldsthepathsmatchingapathnamepattern.thepatternmaycontainsimpleshell-shell unlike fnmatch,filenamesstartingwithadotarespecialcasesthatarenotmatchedby ' * ' and '? ' patterns. If recursive is true,the pattern ' * ' willmatchanyfilesandzeroormoredirectoriesandsubdirectories.' it=_ _ it false (ifrecursiveand _ is recursive ) pathname ) :s=next(it ) skipemptystringassertnotsreturnit2.2. 2

thname, *, recursive=False): pathname:该参数是要匹配的路径recursive:如果是true就会递归的去匹配符合的文件路径,默认是False 返回匹配到的路径列表 2.2.3 glob.glob()函数使用实例

先给出测试使用的目录结构:

test_dir/├── a1.txt├── a2.txt├── a3.py├── sub_dir1│ ├── b1.txt│ ├── b2.py│ └── b3.py└── sub_dir2 ├── c1.txt ├── c2.py └── c3.txt

1、返回目录的路径列表

>>> path_list1 = glob.glob('./test_dir/')>>> path_list['./test_dir/']

2、匹配'./test_dir/*路径下的所有目录和文件,并返回路径列表

>>> path_list2 = glob.glob('./test_dir/*')>>> path_list2['./test_dir/a3.py', './test_dir/a2.txt', './test_dir/sub_dir1', './test_dir/sub_dir2', './test_dir/a1.txt']

3、匹配./test_dir/路径下含有的所有.py文件(不递归)

>>> path_list3 = glob.glob('./test_dir/*.py')>>> path_list3['./test_dir/a3.py'] >>> path_list4 = glob.glob('./test_dir/*/*.py')>>> path_list4['./test_dir/sub_dir1/b2.py', './test_dir/sub_dir1/b3.py', './test_dir/sub_dir2/c2.py']

4、递归的匹配./test_dir/**路径下的所有目录和文件,并返回路径列表

>>> path_list5 = glob.glob('./test_dir/**', recursive=True)>>> path_list5['./test_dir/', './test_dir/a3.py', './test_dir/a2.txt', './test_dir/sub_dir1', './test_dir/sub_dir1/b2.py', './test_dir/sub_dir1/b3.py', './test_dir/sub_dir1/b1.txt', './test_dir/sub_dir2', './test_dir/sub_dir2/c3.txt', './test_dir/sub_dir2/c1.txt', './test_dir/sub_dir2/c2.py', './test_dir/a1.txt'] >>> path_list6 = glob.glob('./test_dir/**/*.py', recursive=True)>>> path_list6['./test_dir/a3.py', './test_dir/sub_dir1/b2.py', './test_dir/sub_dir1/b3.py', './test_dir/sub_dir2/c2.py']

注意:

如果要对某个路径下进行递归,一定要在后面加两个*

>>> path_list = glob.glob('./test_dir/', recursive=True)>>> path_list['./test_dir/'] 2.3 glob.iglob(pathname, recursive=False)函数的使用 2.3.1 glob.iglob()函数的定义 def iglob(pathname, *, recursive=False): """Return an iterator which yields the paths matching a pathname pattern. The pattern may contain simple shell-style wildcards a la fnmatch. However, unlike fnmatch, filenames starting with a dot are special cases that are not matched by '*' and '?' patterns. If recursive is true, the pattern '**' will match any files and zero or more directories and subdirectories. """ it = _iglob(pathname, recursive, False) if recursive and _isrecursive(pathname): s = next(it) # skip empty string assert not s return it 2.3.2 glob.iglob()函数的参数 glob.iglob的参数与glob.glob()一样def iglob(pathname, *, recursive=False): pathname:该参数是要匹配的路径recursive:如果是true就会递归的去匹配符合的文件路径,默认是False 返回一个迭代器,遍历该迭代器的结果与使用相同参数调用glob()的返回结果一致 2.3.3 glob.iglob()函数的使用实例

先给出测试使用的目录结构:

test_dir/├── a1.txt├── a2.txt├── a3.py├── sub_dir1│ ├── b1.txt│ ├── b2.py│ └── b3.py└── sub_dir2 ├── c1.txt ├── c2.py └── c3.txt

正常glob.glob()返回路径列表

>>> path_list4 = glob.glob('./test_dir/*/*.py')>>> path_list4['./test_dir/sub_dir1/b2.py', './test_dir/sub_dir1/b3.py', './test_dir/sub_dir2/c2.py']

现在,使用:glob.iglob()

>>> file_path_iter = glob.iglob('./test_dir/*')>>> print(type(file))<class 'generator'>>>> for file_path in file_path_iter:... print(file_path)..../test_dir/a3.py./test_dir/a2.txt./test_dir/sub_dir1./test_dir/sub_dir2./test_dir/a1.txt>>> 2.4 其他通配符*、?、[]实例 >>> import glob>>> glob.glob('./[0-9].*')['./1.gif', './2.txt']>>> glob.glob('*.gif')['1.gif', 'card.gif']>>> glob.glob('?.gif')['1.gif']>>> glob.glob('**/*.txt', recursive=True)['2.txt', 'sub/3.txt']>>> glob.glob('./**/', recursive=True)['./', './sub/']


♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。