py中的目錄與文件判別代碼
更新時間: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) 篩選出一個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']
判別的應(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) 篩選出一個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']
判別的應(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實現(xiàn)滾動橫版射擊游戲城市之戰(zhàn)
這篇文章主要為大家詳細介紹了python pygame實現(xiàn)滾動橫版射擊游戲城市之戰(zhàn),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-11-11Python抓取通過Ajax加載數(shù)據(jù)的示例
在網(wǎng)頁上,有一些內(nèi)容是通過執(zhí)行Ajax請求動態(tài)加載數(shù)據(jù)渲染出來的,本文主要介紹了使用Python抓取通過Ajax加載數(shù)據(jù),感興趣的可以了解一下2023-05-05Pytorch中實現(xiàn)只導(dǎo)入部分模型參數(shù)的方式
今天小編就為大家分享一篇Pytorch中實現(xiàn)只導(dǎo)入部分模型參數(shù)的方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01python機器學(xué)習(xí)基礎(chǔ)K近鄰算法詳解KNN
這篇文章主要為大家介紹了python機器學(xué)習(xí)基礎(chǔ)K近鄰算法詳解有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2021-11-11VS2022安裝Python開發(fā)環(huán)境的詳細過程
這篇文章主要介紹了VS2022安裝Python開發(fā)環(huán)境,文中用Python實現(xiàn)裴波那契數(shù)列,來感受一下Python的魅力,結(jié)合實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2022-08-08