Skip to content

Commit

Permalink
python related
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown committed Aug 13, 2021
1 parent 63df084 commit 7d6794c
Show file tree
Hide file tree
Showing 7 changed files with 362 additions and 1 deletion.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
- 🐾 [按比例切分数据集](theory/python/splitdataset.md)
- 🐾 [获取当前文件的路径](https://blog.csdn.net/py_tester/article/details/78954034)
- 🐾 [Python模块搜索路径](theory/python/search_path.md)
- 🐾 [Python glob](theory/python/glob.md)

#### 🍃 Python封装工具

Expand All @@ -108,6 +109,10 @@

- 🐾 [QT5](http://code.py40.com/pyqt5/)

#### 🍃 Python 报错

- 🐾 [docker容器python中文乱码](theory/python/zh_error.md)

# 🍭环境篇

## 1⃣️ GPUs
Expand Down Expand Up @@ -188,6 +193,7 @@

- 🐾 [实验管理工具sacred](envs/tools/scared.md)
- 🐾 [bypy 百度云下载python](envs/tools/bypy.md)
- 🐾 [Python日志库loguru](envs/tools/loguru.md)

### 🍬 IDE&Tools安装

Expand Down Expand Up @@ -279,6 +285,7 @@
- [yolo v2](train/detection/yolov2.md)
- [yolo v3](train/detection/yolov3.md)
- [yolo v4](train/detection/yolov4.md)
- [Yolox](train/detection/yolox.md) | [Github](https://github.com/Megvii-BaseDetection/YOLOX)

#### 🍃Segmentation

Expand Down Expand Up @@ -473,6 +480,14 @@

# 🍭项目篇

> 项目结构
>
> 1、config.json格式:参考segmentation
>
> 2、scared格式:参考PANet
>
> 3、Exp自定义格式:参考YoloX
## 1⃣️ 分类

- #### 🐾 [classification](https://github.com/FelixFu520/classification)
Expand All @@ -483,7 +498,7 @@

## 3⃣️ 目标检测

- #### 🐾 [detection](https://github.com/FelixFu520/detection)
- #### 🐾 [YoloX](https://github.com/FelixFu520/YOLOX)

## 4⃣️ 小样本

Expand Down
156 changes: 156 additions & 0 deletions theory/python/glob.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
# Python glob的使用及glob.glob的排序问题

⌚️:2021年08月13日

📚参考

---



```
【摘要】简介: glob是python自带的一个操作文件的相关模块,由于模块功能比较少,所以很容易掌握。用它可以查找符合特定规则的文件路径名。使用该模块查找文件,只需要用到:"*","?","[]"这三个匹配符 星号"*"匹配0个或多个字符 问号"?"匹配任何单个字符 "[]"匹配指定范围内的一个特定字符,如:[0-9]匹配范围内数字,[a-z]和[A-Z]匹配范围内字母 一、...
```

## 简介:

glob是python自带的一个操作文件的相关模块,由于模块功能比较少,所以很容易掌握。用它可以查找符合特定规则的文件路径名。使用该模块查找文件,只需要用到:**"\*","?","[]"**这三个匹配符

1. 星号**"\*"**匹配0个或多个字符
2. 问号**"?"**匹配任何单个字符
3. **"[]"**匹配指定范围内的一个特定字符,如:[0-9]匹配范围内数字,[a-z][A-Z]匹配范围内字母

## 一、glob.glob:

返回所有匹配的文件路径列表。它只有一个参数pathname,定义了文件路径匹配规则,这里可以是绝对路径,也可以是相对路径。

**1、通配符**

星号**"\*"**匹配0个或多个字符

```javascript
import glob
path = glob.glob('C:/Users/huangzh/Desktop/dir/*.txt')
print(path)
"""
结果:
[C:/Users/huangzh/Desktop/dir\\file.txt, C:/Users/huangzh/Desktop/dir\\file1.txt, C:/Users/huangzh/Desktop/dir\\file2.txt, C:/Users/huangzh/Desktop/dir\\fileA.txt, C:/Users/huangzh/Desktop/dir\\fileB.txt]
"""
```

上级目录也可以用星号替代

```javascript
import glob
path = glob.glob('C:/Users/huangzh/Desktop/*/*.*')
for file in path: print(file)
"""
结果:
C:/Users/huangzh/Desktop\dir\file.txt
C:/Users/huangzh/Desktop\dir\file1.txt
C:/Users/huangzh/Desktop\dir\file2.txt
C:/Users/huangzh/Desktop\dir\fileA.txt
C:/Users/huangzh/Desktop\dir\fileB.txt
"""
```

**2、单个字符通配符**

问号**"?"**匹配任何单个字符

```javascript
import glob
path = glob.glob('C:/Users/huangzh/Desktop/dir/file?.txt')
for file in path: print(file)
"""
结果:
C:/Users/huangzh/Desktop/dir\file1.txt
C:/Users/huangzh/Desktop/dir\file2.txt
C:/Users/huangzh/Desktop/dir\fileA.txt
C:/Users/huangzh/Desktop/dir\fileB.txt
"""
```

**3、字符范围**

**"[]"**匹配指定范围内的一个特定字符,如:[0-9]匹配范围内数字,[a-z][A-Z]匹配范围内字母

```javascript
import glob
path = glob.glob('C:/Users/huangzh/Desktop/dir/file[0-9].txt')
for file in path: print(file)
"""
结果:
C:/Users/huangzh/Desktop/dir\file1.txt
C:/Users/huangzh/Desktop/dir\file2.txt
"""

path = glob.glob('C:/Users/huangzh/Desktop/dir/file[A-Z].txt')
for file in path: print(file)
"""
结果:
C:/Users/huangzh/Desktop/dir\fileA.txt
C:/Users/huangzh/Desktop/dir\fileB.txt
"""
```

**一起使用**

```javascript
import glob
path = glob.glob('C:/Users/huangzh/Desktop/dir/*?.t[a-z]t')
for file in path: print(file)
"""
结果:
C:/Users/huangzh/Desktop/dir\file.txt
C:/Users/huangzh/Desktop/dir\file1.txt
C:/Users/huangzh/Desktop/dir\file2.txt
C:/Users/huangzh/Desktop/dir\fileA.txt
C:/Users/huangzh/Desktop/dir\fileB.txt
"""
```

## 二、glob.glob的排序问题

对按顺序生成的文件glob.glob是这样排序的:

```javascript
import glob
path = glob.glob('C:/Users/huangzh/Desktop/dir/*.txt')
for file in path: print(file)
"""
结果:
C:/Users/huangzh/Desktop/dir\file1.txt
C:/Users/huangzh/Desktop/dir\file10.txt
C:/Users/huangzh/Desktop/dir\file100.txt
C:/Users/huangzh/Desktop/dir\file1000.txt
C:/Users/huangzh/Desktop/dir\file2.txt
C:/Users/huangzh/Desktop/dir\file3.txt
"""
```

显然这不是理想的顺序,甚至会影响结果

所以,可以用sorted进行排序

**1、按生成时间排序:**

```javascript
import glob
import os
path = glob.glob('C:/Users/huangzh/Desktop/dir/*.txt')
print(sorted(path, key = os.path.getctime))
"""
结果:
['C:/Users/huangzh/Desktop/dir\\file1.txt', 'C:/Users/huangzh/Desktop/dir\\file2.txt', 'C:/Users/huangzh/Desktop/dir\\file3.txt', 'C:/Users/huangzh/Desktop/dir\\file10.txt', 'C:/Users/huangzh/Desktop/dir\\file100.txt', 'C:/Users/huangzh/Desktop/dir\\file1000.txt']
"""
```

**2、按大小排序:**

```javascript
import glob
import os
path = glob.glob('C:/Users/huangzh/Desktop/dir/*.txt')
sorted(path, key = os.path.getsize)
```
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added theory/python/imgs/12efdasgv
Binary file not shown.
Binary file added theory/python/imgs/image-20210813172244850.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 7d6794c

Please sign in to comment.