程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python file attribute acquisition os Path module

編輯:Python

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.)

1、 Path related

os.path.abspath(path)

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'

os.path.normpath(path)

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.relpath(path, start = os.curdir)

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

2、 Time related

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

3、 Judge relevant

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

4、 other

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


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved