国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

py中的目錄與文件判別代碼

2019-11-25 18:46:57
字體:
來源:轉載
供稿:網友
>>> 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']
>>>
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 龙胜| 闽侯县| 汽车| 远安县| 若尔盖县| 逊克县| 康平县| 随州市| 榆中县| 凤阳县| 札达县| 景洪市| 安达市| 邢台市| 廊坊市| 长白| 平乐县| 隆回县| 客服| 饶平县| 嵊泗县| 海原县| 德江县| 分宜县| 鄂温| 晋江市| 尼木县| 卢湾区| 云梦县| 孟州市| 威海市| 锡林郭勒盟| 滦平县| 同德县| 江川县| 横峰县| 淮阳县| 卫辉市| 饶平县| 安丘市| 进贤县|