>>>importos导入模块
>>>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']
>>>[fforfinos.listdir(dirname)筛选出一个list,存放filename
ifos.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']
>>>[fforfinos.listdir(dirname)筛选出一个list,存放dirname
ifos.path.isdir(os.path.join(dirname,f))]
['Lib','DLLs','include','libs','tcl','Tools','Doc']
判别的应用
>>>os.path.isdir("D:")
True
>>>os.path.isdir("D:python25odbchelper.py")
False
>>>os.path.isfile("D:python25odbchelper.py")
True
当前目录
>>>os.getcwd()
'D:Python25'
通配符的使用,引入glob
IDLE1.2.1
>>>importglob
>>>glob.glob('D:python25*.exe')
['D:python25w9xpopen.exe','D:python25python.exe','D:python25pythonw.exe']
>>>glob.glob('D:python25py*.exe')
['D:python25python.exe','D:python25pythonw.exe']
>>>