Python_ Object-oriented _os.path OS path module

Source: Internet
Author: User

#--os.path os path module
Abspath () converts a relative path to an absolute path
basename () returns the file name section
dirname () Return to the path section
Split () splits the path into separate file parts and path sections into one tuple
join () composes multiple paths and files into a new path
Splitext () splits the path into suffixes and other parts
GetSize () Gets the size of the file
Isdir () detects if the path is a folder
isfile () detects if the path is a file
islink () detection path number no is a link
Getctime () Gets the file creation time (return timestamp)
Getmtime () Gets the last modified time (timestamp) of the file


Getatime () Gets the last access time (timestamp) of the file
Exists () detects if the specified path exists
Isabs () detects if a path is an absolute path
Samefile () detects if the same file is in two paths

====================================================================================================

#--os.path OS path module
Import OS
Import time


#abspath () Converts a relative path to an absolute path
res = Os.path.abspath (".. /")
Print (RES)

#basename () returns the file Name section
Pathvar = "/home/star/ceshi001/dashuaige.txt"
res = Os.path.basename (Pathvar)
Print (RES)

#dirname () Return to the path section
Pathvar = "/home/star/ceshi001/dashuaige.txt"
res = Os.path.dirname (Pathvar)
Print (RES)

#split () splits the path into separate file parts and path sections into one tuple
Pathvar = "/home/star/ceshi001/dashuaige.txt"
res = Os.path.split (Pathvar)
Print (RES)

#join () to make a new path to multiple paths and files
path1 = "/home/star"
path2 = "Download"
Path3 = "2.txt"
res = Os.path.join (path1,path2,path3)
Print (RES)

#splitext () to divide the path into suffixes and other parts
Pathvar = "/home/star/ceshi001/dashuaige.txt"
res = Os.path.splitext (Pathvar)
Print (RES)

#getsize () Gets the size of the file
Pathvar = "/home/star/ceshi0801/star"
res = os.path.getsize (Pathvar)
Print (res) #68040


#isdir () detects if the path is a folder
Pathvar = "/home/star/ceshi0801/"
res = Os.path.isdir (Pathvar)
Print (RES)

#isfile () detects if the path is a file
Pathvar = "/home/star/ceshi0801/star"
res = Os.path.isfile (Pathvar)
Print (RES)

#islink () Detect path number No is a link
Pathvar = "/home/star/ceshi0801/star"
res = Os.path.islink (Pathvar)
Print (RES)

#getctime () Gets the file creation time (return timestamp)
Pathvar = "/home/star/ceshi0801/star"
res = Os.path.getctime (Pathvar)
Zifuchuan = Time.ctime (res)
Print (RES)
Print (Zifuchuan)


#getmtime () Gets the last modified time (timestamp) of the file
Pathvar = "/home/star/ceshi0801/star"
res = Os.path.getmtime (Pathvar)
Zifuchuan = Time.ctime (res)
Print (Zifuchuan)


#getatime () Gets the last access time (timestamp) of the file
Pathvar = "/home/star/ceshi0801/star"
res = Os.path.getatime (Pathvar)
Print (RES)
Zifuchuan =time.ctime (RES)
Print (Zifuchuan)


#exists () detects if the specified path exists
Pathvar = "/home/star/ceshi0801/star"
res = os.path.exists (Pathvar)
Print (RES)

#isabs () detects if a path is an absolute path
Pathvar = "/home/star/ceshi0801/star"
res = Os.path.isabs (".. /")
Print (RES)


#samefile () detects whether the same file is in two paths (refers to the same file)
path1 = "/mnt/hgfs/vm_gongxiang/11223.txt"
path2 = ". /"
res = Os.path.samefile (path1,path2)
Print (RES)

Python_ Object-oriented _os.path OS path module

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.