发布时间:2019-07-13 11:39:20编辑:auto阅读(1823)
python使用os模块判断文件
基础知识
#导入os模块
import os
#判断路径是否存在(True, False)
os.path.exists('/tmp/test')
#判断目标是否是文件(True, False)
os.path.isfile('/tmp/test/file1')
#创建目录(可以递归创建)
os.makedirs('d:/assist/set')遍历目录取出其中文件的方法
import os
def test(dir):
for file in os.listdir(dir):
file_basename = os.path.join(dir, file)
#判断是否是文件,是文件就打印出来,反之,可以用来过滤目录
if os.path.isfile(file_basename):
print file_basename
if os.path.isdir(file_basename):
test(file_basename)
上一篇: python使用数据库
下一篇: Python_基础_01
51295
50745
41342
38154
32624
29522
28372
23243
23211
21534
1608°
2342°
1942°
1883°
2213°
1929°
2613°
4390°
4233°
3005°