发布时间:2019-07-13 11:39:20编辑:auto阅读(2248)
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
55134
41936
36514
31916
26826
26491
25033
20657
16560
15974
3194°
2886°
2979°
3131°
3067°
3343°
3239°
3068°
3060°
3041°