Os.path module is mainly used for file property acquisition, which is often used in programming, the following are several common methods of the module.
>>>ImportOs.path>>> Path ='/home/ethon/doc/file.txt'>>> Os.path.abspath (PATH)#returns the absolute path normalized by path'C:\\home\\ethon\\doc\\file.txt'>>>os.path.split (path) # divides path into directory and file name two tuples return ('/home/ethon/doc','file.txt')>>>os.path.dirname (path) # Returns the directory of path, which is actually the first element of Os.path.split (path) '/home/ethon/doc'>>>os.path.basename (path) # Returns the last file name of path and returns null if path ends with/or \. The second element of Os.path.split (path). 'file.txt'>>> >>> Os.path.commonprefix (['/home/ethon','/home/ethon/doc','/home/ethon/doc/ff'] # returns the longest path common to all paths in the list '/home/ethon
Os.path can also query information about the file (metadata)
Import'/home/ethon/doc/file.txt'os.path.exists (path) # query file exists os.path.getsize (path) # query File size os.path.getatime (path) # query file Last read time os.path.getmtime (path) # query file Last modified time os.path.isfile (path) # does the path point to regular file os.path.isdir (path) # whether the path points to a directory file
Glob Bag
" " In python, the Glob module is used to find matching files in a lookup condition that requires a matching rule in the Unix shell: * : match all? : match one character *. * : Matches such as: [Hello.txt,cat.xls,xxx234s.doc]?. * : matches such as: [1.txt,h.py]?. GIF: matches such as: [X.gif,2.gif] If there is no match, Glob.glob (path) will return an empty list:[""
Demo
Import glob>>> glob.glob ('c:\\pict\\*.*') ['c:\\pict \\Model.txt'c:\\pict\\output.txt'c:\\pict\\ Output.xls'c:\\pict\\pict.exe'c:\\pict\\ picthelp.htm']
Python Learning note 24 (Path and file (Os.path package, Glob package))