py中的目錄與文件判別代碼
更新時(shí)間:2008年07月16日 23:06:02 作者:
python中的判別目錄和文件的腳本
>>> import os 導(dǎo)入模塊
>>> 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) 篩選出一個(gè)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) 篩選出一個(gè)list,存放dirname
if os.path.isdir(os.path.join(dirname, f))]
['Lib', 'DLLs', 'include', 'libs', 'tcl', 'Tools', 'Doc']
判別的應(yīng)用
>>> os.path.isdir("D:\\")
True
>>> os.path.isdir("D:\\python25\\odbchelper.py")
False
>>> os.path.isfile("D:\\python25\\odbchelper.py")
True
當(dāng)前目錄
>>> 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']
>>>
>>> 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) 篩選出一個(gè)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) 篩選出一個(gè)list,存放dirname
if os.path.isdir(os.path.join(dirname, f))]
['Lib', 'DLLs', 'include', 'libs', 'tcl', 'Tools', 'Doc']
判別的應(yīng)用
>>> os.path.isdir("D:\\")
True
>>> os.path.isdir("D:\\python25\\odbchelper.py")
False
>>> os.path.isfile("D:\\python25\\odbchelper.py")
True
當(dāng)前目錄
>>> 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']
>>>
相關(guān)文章
python pygame實(shí)現(xiàn)滾動(dòng)橫版射擊游戲城市之戰(zhàn)
這篇文章主要為大家詳細(xì)介紹了python pygame實(shí)現(xiàn)滾動(dòng)橫版射擊游戲城市之戰(zhàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11python實(shí)現(xiàn)括號(hào)匹配的思路詳解
這篇文章主要介紹了python實(shí)現(xiàn)括號(hào)匹配及匹配格式的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08Python抓取通過(guò)Ajax加載數(shù)據(jù)的示例
在網(wǎng)頁(yè)上,有一些內(nèi)容是通過(guò)執(zhí)行Ajax請(qǐng)求動(dòng)態(tài)加載數(shù)據(jù)渲染出來(lái)的,本文主要介紹了使用Python抓取通過(guò)Ajax加載數(shù)據(jù),感興趣的可以了解一下2023-05-05Pytorch中實(shí)現(xiàn)只導(dǎo)入部分模型參數(shù)的方式
今天小編就為大家分享一篇Pytorch中實(shí)現(xiàn)只導(dǎo)入部分模型參數(shù)的方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01實(shí)時(shí)獲取Python的print輸出流方法
今天小編就為大家分享一篇實(shí)時(shí)獲取Python的print輸出流方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01python機(jī)器學(xué)習(xí)基礎(chǔ)K近鄰算法詳解KNN
這篇文章主要為大家介紹了python機(jī)器學(xué)習(xí)基礎(chǔ)K近鄰算法詳解有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2021-11-11VS2022安裝Python開(kāi)發(fā)環(huán)境的詳細(xì)過(guò)程
這篇文章主要介紹了VS2022安裝Python開(kāi)發(fā)環(huán)境,文中用Python實(shí)現(xiàn)裴波那契數(shù)列,來(lái)感受一下Python的魅力,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08