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