py中的目录与文件判别代码_python教程-查字典教程网
py中的目录与文件判别代码
py中的目录与文件判别代码
发布时间:2016-12-28 来源:查字典编辑
摘要:>>>importos导入模块>>>os.listdir("d:python25")列出所有目录和文件['w9xpopen.exe','RE...

>>>importos导入模块

>>>os.listdir("d:python25")列出所有目录和文件

['w9xpopen.exe','README.txt','NEWS.txt','LICENSE.txt','python.exe','pythonw.exe','Lib','DLLs','include','libs','tcl','Tools','Doc','odbchelper.py','odbchelper.pyc','test.log','sqlConnection.py','sqlConnection.pyc']

>>>dirname="d:python25"支持自定义

>>>os.listdir(dirname)

['w9xpopen.exe','README.txt','NEWS.txt','LICENSE.txt','python.exe','pythonw.exe','Lib','DLLs','include','libs','tcl','Tools','Doc','odbchelper.py','odbchelper.pyc','test.log','sqlConnection.py','sqlConnection.pyc']

>>>[fforfinos.listdir(dirname)筛选出一个list,存放filename

ifos.path.isfile(os.path.join(dirname,f))]

['w9xpopen.exe','README.txt','NEWS.txt','LICENSE.txt','python.exe','pythonw.exe','odbchelper.py','odbchelper.pyc','test.log','sqlConnection.py','sqlConnection.pyc']

>>>[fforfinos.listdir(dirname)筛选出一个list,存放dirname

ifos.path.isdir(os.path.join(dirname,f))]

['Lib','DLLs','include','libs','tcl','Tools','Doc']

判别的应用

>>>os.path.isdir("D:")

True

>>>os.path.isdir("D:python25odbchelper.py")

False

>>>os.path.isfile("D:python25odbchelper.py")

True

当前目录

>>>os.getcwd()

'D:Python25'

通配符的使用,引入glob

IDLE1.2.1

>>>importglob

>>>glob.glob('D:python25*.exe')

['D:python25w9xpopen.exe','D:python25python.exe','D:python25pythonw.exe']

>>>glob.glob('D:python25py*.exe')

['D:python25python.exe','D:python25pythonw.exe']

>>>

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新python学习
    热门python学习
    脚本专栏子分类