


Simply master the usage of glob module to find file paths in Python
glob使用UNIX shell规则查找与一个模式匹配的文件名。只要程序需要查找文件系统中名字与某个模式匹配的一组文件,就可以使用这个模块。
glob的模式规则与re模块使用的正则表达式不相同。glob模式遵循标准UNIX路径扩展规则。只是用几个特殊字符来实现两个不同的通配符和字符区间。模式规则要应用于文件名中的段。模式中的路径可以是相对路径或绝对路径。
shell变量名和波浪线都不会扩展。
基本用法
1.glob.glob(pathname), 返回所有匹配的文件路径列表。它只有一个参数pathname,定义了文件路径匹配规则,这里可以是绝对路径,也可以是相对路径。
2.glob.iglob(pathname), 获取一个可编历对象,使用它可以逐个获取匹配的文件路径名。与glob.glob()的区别是:glob.glob同时获取所有的匹配路径,而glob.iglob一次只获取一个匹配路径。
3.eg:
import glob print glob.glob(r'E:\*\*.doc') print glob.glob(r'.\*.py') f = glob.iglob(r'.\*.py') for py in f: print py
运行结果:
['E:\\test_file\\adplus.doc'] ['.\\perfrom_test.py', '.\\pyTest.py', '.\\simulation_login.py', '.\\widget.py', '.\\__init__.py'] .\perfrom_test.py .\pyTest.py .\simulation_login.py .\widget.py .\__init__.py
下面我们分知识点详细来讲:
通配符
星号匹配一个文件名段中的0个或多个字符。
import glob for name in glob.glob('tmp/*'): print name
这个模式会匹配所有的路径名,但是不会递归搜索到子目录。
>>> ================================ RESTART ================================ >>> tmp\checklog_status.sh tmp\check_Adwords_v1.2.sh tmp\check_traffic.sh tmp\cut_nginxlog_V1.2.sh tmp\ip_conn.sh tmp\ip_keepalive.sh tmp\nagios使用手册.doc tmp\nmap_ping tmp\nrpe_install-1.3.sh tmp\one tmp\syn.sh tmp\zabbix_agentd_2.0.10_win_V1.2.bat tmp\zabbix_agentd_2.0.8_V1.3.sh tmp\工作内容.doc
要列出子目录中的文件,必须把子目录包含在模式中。
import glob print 'Name explicitly:' for name in glob.glob('tmp/one/*'): print '\t', name print 'Name with wildcard:' for name in glob.glob('tmp/*/*'): print '\t', name
第一种情况显示列出子目录名,第二种情况则依赖一个通配符查找目录。
>>> ================================ RESTART ================================ >>> Name explicitly: tmp/one\another.txt tmp/one\file.txt Name with wildcard: tmp\one\another.txt tmp\one\file.txt
单字节通配符
问号会匹配文件名中该位置的单个字符。
import glob for name in glob.glob('tmp/chec?_traffic.sh'): print name
>>> ================================ RESTART ================================ >>> tmp\check_traffic.sh
字符区间
使用字符区间([a-z]),可以匹配多个字符中的一个字符。
import glob for name in glob.glob('tmp/one/[a-z]*'): print name
区间可以匹配所有小写字母。
>>> ================================ RESTART ================================ >>> tmp/one\another.txt tmp/one\file.txt

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.
