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

首頁 > 編程 > Python > 正文

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

2020-02-23 04:39:18
字體:
來源:轉載
供稿:網友
>>> 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']
>>>
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 秦皇岛市| 长葛市| 十堰市| 鱼台县| 洛南县| 池州市| 涞水县| 宁海县| 建平县| 宝鸡市| 五莲县| 年辖:市辖区| 南充市| 沾益县| 永德县| 井冈山市| 阜新| 桐城市| 宜都市| 怀来县| 板桥市| 双桥区| 塘沽区| 老河口市| 开封市| 嵊州市| 醴陵市| 安龙县| 昌江| 天柱县| 寻甸| 井研县| 无为县| 辽源市| 中宁县| 拉孜县| 张家港市| 会东县| 台东县| 新巴尔虎左旗| 庐江县|