Python file path operations

Source: Internet
Author: User
Importos.pathpathhomevameidocfile.txt print (OS. path. basename (path) # query the file name print (OS. path. dirname (path) # query the directory infoos contained in the path. path. split (path) # split the path into two parts: file name and directory... import OS. path

Path = '/home/vamei/doc/file.txt'


Print (OS. path. basename (path) # query the file name contained in the path

Print (OS. path. dirname (path) # query the directories contained in the path


Info = OS. path. split (path) # split the path into two parts: file name and directory.

Print (info)

Path2 = OS. path. join ('/', 'home', 'vamei', 'Doc', 'file1.txt ') # Use the directory name and file name to form a path string. the string is spliced and output: /home \ vamei \ doc \ file1.txt


P_list = [path, path2]

Print (OS. path. commonprefix (p_list) # query the common parts of multiple paths


OS. path. normpath (path) # Remove redundancy in the path. For example, '/home/vamei/...' is converted to '/home'



Print (OS. path. exists (path) # Query whether a file exists


Print (OS. path. getsize (path) # query the file size

Print (OS. path. getatime (path) # query the last file read time

Print (OS. path. getmtime (path) # query the last file modification time


Print (OS. path. isfile (path) # whether the path points to a regular file

Print (OS. path. isdir (path) # whether the path points to a directory file

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.