python-os模块使用
Posted lovejobs
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python-os模块使用相关的知识,希望对你有一定的参考价值。
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模块使用的主要内容,如果未能解决你的问题,请参考以下文章