Skip to content

Commit 9dcfca9

Browse files
committed
新增遍历目录下的文件测试
1 parent 379e3e0 commit 9dcfca9

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

test/test_file.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python
2+
# encoding: utf-8
3+
4+
"""
5+
@author: zhanghe
6+
@software: PyCharm
7+
@file: test_file.py
8+
@time: 16-1-9 上午11:24
9+
"""
10+
11+
import os
12+
import os.path
13+
14+
root_dir = '/tmp' # 指明被遍历的文件夹
15+
16+
for dir_parent, dir_names, file_names in os.walk(root_dir): # 三个参数:分别返回1.父目录 2.所有文件夹名字(不含路径) 3.所有文件名字
17+
for dir_name in dir_names: # 输出文件夹信息
18+
print "dir_parent is:" + dir_parent
19+
print "dir_name is" + dir_name
20+
21+
for file_name in file_names: # 输出文件信息
22+
print "dir_parent is:" + dir_parent
23+
print "file_name is:" + file_name
24+
print "the full name of the file is:" + os.path.join(dir_parent, file_name) # 输出文件路径信息
25+
26+
# 获取指定目录下的所有文件和目录名,列表形式
27+
print os.listdir(root_dir)
28+
29+
root_dir = '/tmp'
30+
for file_name in [item for item in os.listdir(root_dir) if item.startswith('resume-') and item.endswith('.html')]:
31+
file_path = os.path.join(root_dir, file_name)
32+
print file_path
33+
with open(file_path, 'rb') as f:
34+
html = f.read().decode('utf-8')
35+
print html
36+
37+
38+
if __name__ == '__main__':
39+
pass

0 commit comments

Comments
 (0)