Python-os module Use

Source: Internet
Author: User
Tags glob

1. Merging paths

Os.path.join ("c:\\music\\ap\\0","mav.mp3")'  C:\\music\\ap\\0\\mav.mp3'

2. Find the User directory

Os.path.expanduser ("~")'c:\\users\\administrator' 

3. Split path name and file name

Os.path.split ("theWolf of the c:\\music\\ap\\ sheepskin." MP3") (The Wolf of the'c:\\music\\ap' sheepskin. MP3')#  tuple tuples

4. Separating path names and filenames by defining tuples

>>> (Filepath,filename) =os.path.split ("theWolf of c:\\music\\ap\\ sheepskin." MP3")>>> filepath'c:\\music\\ap'>> > filename' sheepskin Wolf. MP3'

5. Separate file names and extensions

>>> filename' sheepskin Wolf. MP3'>>> (shortname,extensionname) =os.path.splitext (filename)>>>  ShortName ' sheepskin wolf '>>> extensionname'. MP3'

6. List all folders and files at this level under the path

 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 into the shell spot welder (schematic) a0.pdf ', ' 1805-18 SUWLARKJ14 Huipenbo (servo line changed from ultra-high flex to slender). pdf ', ' 36 0SANDBOX ', ' acadminidump.dmp ', ' AX NF ZZ ', ' Boot ', ' bootmgr ', ' config.msi ', ' Documents and Settings ', ' Downloads ', ' Driver S ', ' 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. Determine if a path is a file or a directory

>>> [F forFinchOs.listdir ("c:\\")ifOs.path.isfile (Os.path.join ("c:\\", F))] ['1805-18 SUWLARKJ14 into the Shell spot welding machine (schematic) a0.pdf','1805-18 SUWLARKJ14 Huipenbo (servo line changed from ultra-high flex to slender). pdf','acadminidump.dmp','bootmgr','hangcha.pdf','Offline_ftninfo.txt','Pagefile.sys','Uwscan_n.ini','Weldwave.ini'] File judgment>>> [F forFinchOs.listdir ("c:\\")ifOs.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'] folder to determine

8. Find a specific file

>>> Os.listdir ("d:\\")['$RECYCLE. BIN','1.json.wmv','1805-18 SUWLARKJ14 in-shell pre-welding machine (IO configuration table)-A0.pdf','1805-18 suwlarkj14 into the shell spot welding machine A0-1.bak','1805-18 suwlarkj14 into the shell spot welding machine A0-1.DWG','1805-18 SUWLARKJ14 into the shell spot welding machine A0.DWG','1805-18 Rui Pu into the shell spot welding machine A0_2018_06_21.DWG','fat_ Modification of 1805-28 suwlarkj13-02 in-shell pre-welding machine two. xlsx','2018-related requirements for collection of dues. RAR','360Downloads','360MoveData','360 Secure Browser Download','6PPM in-shell pre-welding machine','Adobe','Androidstudio','Asmpg','Baiduyundownload','C # Notes','CAD','cat_200_300.jpg','Datastream.txt','Icon.png','Irisdata.txt','Lab.dat','Lbview','Lib','LVS.txt','Map.txt','Masm32','Mels','MES system data acquisition requirements Table v1.05 Chengdu, the laser sealing (laser cleaning). xlsx','MFC class diagram. PNG','Mkspecs','Mmp.txt','Mmpp.txt','MSOCache','MySQL','Open.reg','openok.reg-copy. txt','Openok.reg.txt','Plugins','pp. PNG','py','python','QT','Sanliuo','Sheet.xls','Skypee','SolidWorks','Stormmedia','System Volume Information','VC','vs','ye_applications','Entertainment','Training table for sealing nail welding table. xlsx','Embedded Tools Software','Tool Software','Registration Procedure','Welding Barcode','User Directory','Electrical Professional drawings Audit self-Test table fr-02-17066 (1). xls','errors in the program specification. Doc','Programming Software','Automation Manual','all data and parameters of the West Dan Fu seal nail','Thunder Download','Code of Procedure for silver-lung seal nails-docx']>>>ImportGlob>>> 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-copy. txt','D:\\openok.reg.txt']

Python-os module Use

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.