手机
当前位置:查字典教程网 >脚本专栏 >python >py中的目录与文件判别代码
py中的目录与文件判别代码
摘要:>>>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']

>>>

【py中的目录与文件判别代码】相关文章:

一则python3的简单爬虫代码

python实现跨文件全局变量的方法

python将人民币转换大写的脚本代码

用python分割TXT文件成4K的TXT文件

python合并文本文件示例

删除目录下相同文件的python代码(逐级优化)

python实现人人网登录示例分享

python操作xml文件详细介绍

python实现的登录和操作开心网脚本分享

python在windows下实现备份程序实例

精品推荐
分类导航