python-os模塊使用
阿新 • • 發佈:2018-09-04
user mini wave 目錄 瀏覽器 x86 這一 mys 瀏覽器下載
1.合並路徑
os.path.join("c:\\music\\ap\\0","mav.mp3") ‘c:\\music\\ap\\0\\mav.mp3‘
2.尋找用戶目錄
os.path.expanduser("~") ‘C:\\Users\\Administrator‘
3.分割路徑名和文件名
os.path.split("c:\\music\\ap\\羊皮的狼.MP3") (‘c:\\music\\ap‘, ‘羊皮的狼.MP3‘)#元組tuple
4.通過定義元組來分開路徑名和文件名
>>> (filepath,filename)=os.path.split("c:\\music\\ap\\羊皮的狼.MP3") >>> filepath ‘c:\\music\\ap‘ >>> filename ‘羊皮的狼.MP3‘
5.分開文件名和擴展名
>>> filename ‘羊皮的狼.MP3‘ >>> (shortname,extensionname)=os.path.splitext(filename) >>> shortname ‘羊皮的狼‘ >>> extensionname ‘.MP3‘
6.列出路徑下的這一級的所有文件夾和文件
os.listdir("d:\\mysql\\") [‘lib‘, ‘my 2017-12-10 1949.ini.bak‘, ‘my 2017-12-10 1953.ini.bak‘, ‘my.ini‘]
>>> os.listdir("c:\\")
[‘$360Section‘, ‘$Recycle.Bin‘, ‘1805-18 SUWLARKJ14 入殼點焊機 (原理圖) A0.pdf‘, ‘1805-18 SUWLARKJ14 惠鵬博 (伺服線由超高柔更換為高柔).pdf‘, ‘360SANDBOX‘, ‘acadminidump.dmp‘, ‘AX NF ZZ‘, ‘Boot‘, ‘bootmgr‘, ‘Config.Msi‘, ‘Documents and Settings‘, ‘Downloads‘, ‘Drivers‘, ‘DRMsoft‘, ‘FeigeDownload‘, ‘Google‘, ‘hangcha.pdf‘, ‘HWUpdates‘, ‘Intel‘, ‘kingdeeplm‘, ‘MSOCache‘, ‘OEMSF‘, ‘offline_FtnInfo.txt‘, ‘pagefile.sys‘, ‘Program Files‘, ‘Program Files (x86)‘, ‘ProgramData‘, ‘QMDownload‘, ‘Skypee‘, ‘SSE138folder‘, ‘System Volume Information‘, ‘System32Folder‘, ‘TEMP‘, ‘Users‘, ‘uwscan_n.ini‘, ‘WeldWave.ini‘, ‘Windows‘, ‘_ISTMP1.DIR‘]
7.判斷一個路徑是文件還是目錄
>>> [f for f in os.listdir("c:\\") if os.path.isfile(os.path.join("c:\\",f))] [‘1805-18 SUWLARKJ14 入殼點焊機 (原理圖) A0.pdf‘, ‘1805-18 SUWLARKJ14 惠鵬博 (伺服線由超高柔更換為高柔).pdf‘, ‘acadminidump.dmp‘, ‘bootmgr‘, ‘hangcha.pdf‘, ‘offline_FtnInfo.txt‘, ‘pagefile.sys‘, ‘uwscan_n.ini‘, ‘WeldWave.ini‘] 文件判斷 >>> [f for f in os.listdir("c:\\") if os.path.isdir(os.path.join("c:\\",f))] [‘$360Section‘, ‘$Recycle.Bin‘, ‘360SANDBOX‘, ‘AX NF ZZ‘, ‘Boot‘, ‘Config.Msi‘, ‘Documents and Settings‘, ‘Downloads‘, ‘Drivers‘, ‘DRMsoft‘, ‘FeigeDownload‘, ‘Google‘, ‘HWUpdates‘, ‘Intel‘, ‘kingdeeplm‘, ‘MSOCache‘, ‘Program Files‘, ‘Program Files (x86)‘, ‘ProgramData‘, ‘QMDownload‘, ‘Skypee‘, ‘SSE138folder‘, ‘System Volume Information‘, ‘System32Folder‘, ‘TEMP‘, ‘Users‘, ‘Windows‘, ‘_ISTMP1.DIR‘] 文件夾判斷
8.查找特定的文件
>>> os.listdir("d:\\") [‘$RECYCLE.BIN‘, ‘1.json.wmv‘, ‘1805-18 SUWLARKJ14 入殼預焊機 (IO配置表) - A0.pdf‘, ‘1805-18 suwlarkj14入殼點焊機 a0-1.bak‘, ‘1805-18 suwlarkj14入殼點焊機 a0-1.dwg‘, ‘1805-18 SUWLARKJ14入殼點焊機 A0.dwg‘, ‘1805-18瑞浦入殼點焊機 A0_2018_06_21.dwg‘, ‘1805-28 SUWLARKJ13-02 入殼預焊機FAT_改二.xlsx‘, ‘2018-關於收繳黨費的相關要求.rar‘, ‘360Downloads‘, ‘360MoveData‘, ‘360安全瀏覽器下載‘, ‘6PPM入殼預焊機‘, ‘adobe‘, ‘androidstudio‘, ‘asmpg‘, ‘BaiduYunDownload‘, ‘c#筆記‘, ‘CAD‘, ‘cat_200_300.jpg‘, ‘datastream.txt‘, ‘icon.png‘, ‘irisdata.txt‘, ‘lab.dat‘, ‘lbview‘, ‘lib‘, ‘LVS.txt‘, ‘map.txt‘, ‘masm32‘, ‘mels‘, ‘MES系統數據采集需求表v1.05成都銀隆_激光封口(激光清洗).xlsx‘, ‘MFC類圖.png‘, ‘mkspecs‘, ‘mmp.txt‘, ‘mmpp.txt‘, ‘MSOCache‘, ‘mysql‘, ‘open.reg‘, ‘openok.reg - 副本.txt‘, ‘openok.reg.txt‘, ‘plugins‘, ‘pp.PNG‘, ‘py‘, ‘python‘, ‘qt‘, ‘sanliuo‘, ‘SHEET.xls‘, ‘Skypee‘, ‘solidworks‘, ‘StormMedia‘, ‘System Volume Information‘, ‘vc‘, ‘vs‘, ‘YE_Applications‘, ‘娛樂‘, ‘密封釘焊接工作臺培訓表.xlsx‘, ‘嵌入式工具軟件‘, ‘工具軟件‘, ‘戶籍轉回辦理手續‘, ‘焊接條碼‘, ‘用戶目錄‘, ‘電氣專業圖紙審核自檢表FR-02-17066(1).xls‘, ‘程序規範中的錯誤.doc‘, ‘編程軟件‘, ‘自動化手冊‘, ‘西丹孚密封釘全部資料與參數‘, ‘迅雷下載‘, ‘銀隆密封釘程序規範.docx‘] >>> import glob >>> glob.glob("d:\\*.txt") [‘d:\\datastream.txt‘, ‘d:\\irisdata.txt‘, ‘d:\\LVS.txt‘, ‘d:\\map.txt‘, ‘d:\\mmp.txt‘, ‘d:\\mmpp.txt‘, ‘d:\\openok.reg - 副本.txt‘, ‘d:\\openok.reg.txt‘]
python-os模塊使用