Python Learning Note 9: path and file of the logo library

Source: Internet
Author: User
Tags glob

A Os.path bag
And look at the code:
Import os.pathpath= "/home/user/document/test.txt" Print (path)    # The file name that is contained in the query path print ( Os.path.dirname (Path)     # The directory contained in the query path info = os.path.split       # divides the path into a file name and a directory of two parts, placed in a table to return print (info) path2 = Os.path.join ('/', ' home ', ' User ', ' Document ', ' file1.txt ')  # Use directory name and file name to form a path string print (path2) p_list = [path, path2] Print (Os.path.commonprefix (p_list))    # Querying common parts of multiple paths


Output:
Test.txt
/home/user/document
('/home/user/document ', ' test.txt ')
/home\user\document\file1.txt
/Home



In addition, there are:
Print (path) # os.path.exists query for file existence
Print (path) # query file size os.path.getsize
Print (path) # Os.path.getatime The time the file was last read
Print (path) # Os.path.getmtime The time the file was last modified
Print (path) # path os.path.isfile to regular file
Print (path) # path os.path.isdir to directory file


Two glob bags
Glob package The most common method is only one, Glob.glob (). The function of this method is similar to LS in Linux.
Accepts a Linux-style file name format expression (filename pattern,
Lists all files that conform to the expression (similar to regular expressions) and returns all filenames in one table.
So Glob.glob () is a good way to query files in a directory.


The syntax of the file name expression is different from the regular expression of Python itself, and the corresponding relationship is roughly as follows:


Filename Pattern expression Python Regular expression


*                                .*


? .


[0-9] Same


[A-E] Same


[^MNP] Same

Python Learning Note 9: path and file of the logo library

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.