[Python basic]010.os Module (2)

Source: Internet
Author: User

OS Modules (2)

    • Introduced
    • Os
      • Constant
      • Path
      • Determining Path Properties
      • Path transformation
      • File properties
      • Same file
Introduced

- os.path module, the main processing 路径操作 , contains a variety of processing files and file name method.

Os.path Constants
    • os.path.sepPath Delimiter (Unix for / , win for \\ )
    • os.path.pathsepDelimiter between multiple paths, more for environment variables (Unix for : , win for ; )
    • os.path.extsepThe suffix name symbol is typically.
Path
    • os.path.splitSplit path to directory name and file name
    • os.path.dirnameDirectory Name
    • os.path.basenameFilename
    • os.path.splitextSplit path to file and extension
    • os.path.joinPath combination

Code

ImportOsfilename='/users/superdo/test.txt'#Split Path_dir, _file =os.path.split (filename)Print_dir#/users/superdoPrint_file#Test.txt#Directory namePrintOs.path.dirname (filename)#/users/superdo#file namePrintOs.path.basename (filename)#Test.txt#Extended Name_filename, _ext =os.path.splitext (filename)Print_filename#/users/superdo/testPrint_ext#. txt#Path Combinationf = Os.path.join ('Users','Superdo','Test.txt')PrintF#Users/superdo/test.txt

Determining Path Properties
    • os.path.existsWhether the file exists
    • os.path.isdirWhether it is a directory
    • os.path.isfileWhether it is a file
    • os.path.islinkis the connection file
    • os.path.ismountWhether to mount the file
    • os.path.isabsis the absolute path

Code

ImportOsfilename='/users/superdo/test.txt'Printos.path.exists (filename)#Judging existencePrintOs.path.isdir (filename)#Judging folderPrintOs.path.isfile (filename)#Judging FilesPrintOs.path.islink (filename)#Determine the connection filePrintOs.path.ismount (filename)#Determining Mount FilesPrintOs.path.isabs (filename)#Determine absolute path

Path transformation
    • os.path.relpathRelative path
    • os.path.abspathAbsolute path
    • os.path.normpathStandardized paths
    • os.path.commonprefixGet a common path

Code

ImportOsfilename='/users/superdo/ac/. /path.txt'#Get relative pathPrintOs.path.relpath (filename)#gets the path relative to the current pathPrintOs.path.relpath (filename,'/users')#get the path relative to/users#Absolute PathPrintos.path.abspath (filename)#Standardized PathsPrintos.path.normpath (filename)#get a common path in multiple pathsF1='/users/superdo/zergling/file.txt'F2='/users/superdo/ac/file.txt'f3='/users/superdo/horse/file.txt'PrintOs.path.commonprefix ([F1, F2, F3])#/users/superdo/

File properties
    • os.path.getatimeAccess time (obtained from Os.stat)
    • os.path.getmtimeModification time (obtained from Os.stat)
    • os.path.getctimeCreation time (obtained from Os.stat)
    • os.path.getsizeFile size (obtained from Os.stat)

Code

Import OS Import  '/users/superdo/test.txt'print  os.path.getatime (filename) Print os.path.getmtime (filename) Print os.path.getctime (filename) print os.path.getsize (filename)

Same file
    • os.path.samefileCompare files
    • os.path.sameopenfileCompare open files
    • os.path.samestatCompare Files Stat

Code

Import'/users/superdo/file.txt'/users/superdo/ File.txt'print== Open (F2)print=  = os.stat (f2)print os.path.samestat (FS1, FS2)

This site article is for baby bus SD. Team Original, reproduced must be clearly noted: (the author's official website: Baby bus )
Reprinted from "Baby bus Superdo Team" original link: http://www.cnblogs.com/superdo/p/4719191.html

[Python basic]010.os Module (2)

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.