Skip to content

Commit bafd0a5

Browse files
committed
新增md文件
新增md文件
1 parent 50f266a commit bafd0a5

File tree

6 files changed

+94
-0
lines changed

6 files changed

+94
-0
lines changed

python_basic/main.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
## Python基础
22
1. [时间格式转换](时间格式转换.md)
3+
2. [字节串与十六进制转换](字节串与十六进制转换.md)
4+
3. [struct-序列化与反序列化](序列化与反序列化.md)
5+
4. [文件路径处理](文件路径处理.md)
6+
5. [创建临时文件](创建临时文件.md)
7+
6. [文件目录扫描](文件目录扫描.md)

python_basic/创建临时文件.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## 创建临时文件
2+
```python
3+
import tempfile
4+
import os
5+
tmpfd, tmp_file_path = tempfile.mkstemp()
6+
print(tmpfd) #文件描述符
7+
print(tmp_file_path) #临时文件的绝对路径
8+
os.close(tmpfd) #关闭文件
9+
10+
# 防止占用资源
11+
# mktemp用于返回一个临时文件的路径,但并不创建该临时文件。
12+
tempfile.mktemp()
13+
```
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
## “十六进制字符串”转“字节串”
2+
```python
3+
data = '7c900030000001780000000030303035'
4+
bytes().fromhex(data)
5+
b'|\x90\x000\x00\x00\x01x\x00\x00\x00\x000005'
6+
```
7+
## “字节串”转“十六进制字符串”
8+
### 方法一
9+
```python
10+
def bytes_to_hex_str(bytes_data):
11+
if bytes_data:
12+
return ''.join(['%02x' % b for b in bytes_data])
13+
else:
14+
return ''
15+
16+
data = b'\\\x00?\x00?\x00\\\x00E\x00:\x00'
17+
# 5c003f003f005c0045003a00
18+
print(bytes_to_hex_str(data))
19+
```
20+
### 方法二
21+
```python
22+
import binascii
23+
data = b'\\\x00?\x00?\x00\\\x00E\x00:\x00'
24+
# 5c003f003f005c0045003a00
25+
print(binascii.hexlify(data).decode('utf-8'))
26+
```
27+
## “字节”转换成“十六进制”表示
28+
```python
29+
# K是ascii中可见字符,所以编辑器在碰到0x4b的时候就把0x4b转换成了b'K'
30+
hex(ord(b'K')) # 0x4b
31+
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## 反序列化
2+
* unpack,字节转换成常用数据类型
3+
```python
4+
data = '7c90000B00001818000000003235303630303035'
5+
import struct
6+
# (11,)
7+
struct.unpack('>H', bytes.fromhex(data)[2:4])
8+
```
9+
## 序列化
10+
* pack, 常用数据类型转换成字节串
11+
```python
12+
s = struct.Struct('III')
13+
# b'\x0c\x00\x00\x00\x0f\x00\x00\x00\x10\x00\x00\x00'
14+
s.pack(12,15,16)
15+
```

python_basic/文件目录扫描.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## 文件目录扫描
2+
#### 扫描目录下所有文件,递归,但是文件数量多的情况下比较耗时:
3+
```python
4+
all_files_list = []
5+
for root, dirs, files in os.walk(root_dir):
6+
for file in files:
7+
all_files_list.append(os.path.join(root, file))
8+
```
9+
#### 高效率扫描,只针对单目录结构,不递归:
10+
```python
11+
pkg_file_list = []
12+
all_file_generator = os.scandir(root_dir)
13+
for p in all_file_generator:
14+
pkg_file_list.append(p.path)
15+
```

python_basic/文件路径处理.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## 文件路径处理
2+
```python
3+
import os
4+
path = r'C:\Users\daacheng\Desktop\bl_dev\imsiimei_whcs_bh_yyzkdw_dzwl_20180510103417_120.bcp'
5+
# 获取文件名称 imsiimei_whcs_bh_yyzkdw_dzwl_20180510103417_120.bcp
6+
print(os.path.basename(path))
7+
# 获取文件所在文件夹的路径 C:\Users\daacheng\Desktop\bl_dev
8+
print(os.path.dirname(path))
9+
# 返回元组,包含dirname和basename
10+
# ('C:\\Users\\daacheng\\Desktop\\bl_dev', 'imsiimei_whcs_bh_yyzkdw_dzwl_20180510103417_120.bcp')
11+
print(os.path.split(path))
12+
# 返回元组,包含文件路径名和文件扩展名
13+
# ('C:\\Users\\daacheng\\Desktop\\bl_dev\\imsiimei_whcs_bh_yyzkdw_dzwl_20180510103417_120', '.bcp')
14+
print(os.path.splitext(path))
15+
```

0 commit comments

Comments
 (0)