As a resident , We all have our own ID, Record our names , Gender , Date of birth , Registered residence and other information , To some extent , This information is our personal attribute , Get these attributes to know our personal information . As a file stored in physical space , From the moment it was born , It is also endowed with a number of unique attributes , Such as file name , Extension , Storage path , Creation and modification time, etc , How can we accurately obtain these attributes ?
Python Standard library os modular Provides os.path This powerful sub module , You can easily get the above properties of the file . Of course , With Python Iteration , This sub module is gradually being pantlib2 The trend of standard library replacement .
os.path Sub library to path For entrance , For manipulating and processing file paths , The common commands are as follows :
( For convenience , This article is based on the file stored in the document test.py For example , Its full path is /home/probobo/Documents/test.py.)
return path The absolute path in the current system .
>>> import os
>>> os.path.abspath('test.py')
'/home/probobo/test.py'
The problem is coming. , You will find that what you get is not what you want , The result is not a file test.py The absolute path of . This is mainly because the absolute path displayed by this command is actually Current working directory The absolute path of , That is to say os.path.abspath() The result depends on os.getcwd():
>>> os.getcwd()
'/home/probobo'
If test.py For the current project , be os.path.abspath() Can perfectly display its absolute path :
>>> import os
>>> os.path.abspath('test.py')
'/home/probobo/Documents/test.py'
>>> os.getcwd()
'/home/probobo/Documents'
normalization path The representation of , All redundant delimiters and during up-level Collapse all references .Linux The system uses a slash (/) Split the path ,Windows The system uses double backslashes (\\).
>>> os.path.normpath('/home//probobo/./Documents/test.py')
'/home/probobo/Documents/test.py'
os.path.basename(path) Return the filename
os.path.commonprefix(list) return list( Multiple paths ) in , all path The longest path in common
os.path.dirname(path) Return file path
os.path.getatime(path) Return to the most recent visit time ( Floating point seconds )
os.path.getmtime(path) Return the latest file modification time
os.path.getctime(path) Return file path Creation time
os.path.exists(path) If the path exists, it returns True, Path corruption return False
os.path.isabs(path) Determine if it's an absolute path
os.path.isfile(path) Determine whether the path is a file
os.path.isdir(path) Determine whether the path is a directory
os.path.islink(path) Determine whether the path is a link
os.path.ismount(path) Determine if the path is a mount point
os.path.samefile(path1, path2) Determine whether the directory or file is the same
os.path.sameopenfile(fp1, fp2) Judge fp1 and fp2 Whether to point to the same file
os.path.samestat(stat1, stat2) Judge stat tuple stat1 and stat2 Whether to point to the same file
os.path.getsize(path) Return file size , Returns an error if the file does not exist
os.path.join(path1[, path2[, ...]]) Combine directory and filename into a path
os.path.split(path) Divide the path into dirname and basename, Returns a tuple
Catalogue des articlesPréfaceD