国产探花免费观看_亚洲丰满少妇自慰呻吟_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']
>>>
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 五台县| 山西省| 康定县| 二手房| 炉霍县| 淮滨县| 景泰县| 荆门市| 兴和县| 广州市| 礼泉县| 侯马市| 马鞍山市| 任丘市| 永定县| 灌阳县| 措美县| 库尔勒市| 黄山市| 遂川县| 磐石市| 敖汉旗| 华亭县| 曲阜市| 竹溪县| 盖州市| 营山县| 郸城县| 临夏县| 徐州市| 镇巴县| 甘孜| 永春县| 达州市| 奉新县| 托克逊县| 马尔康县| 肥西县| 察隅县| 汝南县| 丰顺县|