没有合适的资源?快使用搜索试试~ 我知道了~
>>> import os 导入模块 >>> 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’, ‘
资源推荐
资源详情
资源评论
py中的目录与文件判别代码中的目录与文件判别代码
>>> import os 导入模块
>>> 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’]
>>> [f for f in os.listdir(dirname) 筛选出一个list,存放filename
if os.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’]
>>> [f for f in os.listdir(dirname) 筛选出一个list,存放dirname
if os.path.isdir(os.path.join(dirname, f))]
[‘Lib’, ‘DLLs’, ‘include’, ‘libs’, ‘tcl’, ‘Tools’, ‘Doc’]
判别的应用
>>> os.path.isdir(“D:\”)
True
>>> os.path.isdir(“D:\python25\odbchelper.py”)
False
>>> os.path.isfile(“D:\python25\odbchelper.py”)
True
当前目录
>>> os.getcwd()
‘D:\Python25’
通配符的使用,引入glob
IDLE 1.2.1
>>> import glob
>>> glob.glob(‘D:\python25\*.exe’)
[‘D:\python25\w9xpopen.exe’, ‘D:\python25\python.exe’, ‘D:\python25\pythonw.exe’]
>>> glob.glob(‘D:\python25\py*.exe’)
[‘D:\python25\python.exe’, ‘D:\python25\pythonw.exe’]
>>>
资源评论
weixin_38689027
- 粉丝: 5
- 资源: 888
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功