Python common modules os.path

Time:2020-9-15

os.path.abspath(path)

Enter relative path and return absolute path

Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import os
>>> print(os.path.abspath("."))
C:\Users914\AppData\Local\Programs\Python\Python37

os.path.basename(path)

Returns the name of the file or directory at the end of the path. String processing alone does not matter whether the path exists or not

>>> import os
>>> os.path.basename("E:\abc\efg.txt")
'efg.txt'

os.path.dirname(path)

and os.path.basename On the contrary, the part of the return path after removing the last file or directory name is also a simple string processing

>>> import os
>>> os.path.dirname("E:\abc\efg.txt")   
'E:\abc'

os.path.split(path)

Simple string processing, split the path, return the tuple composed of its directory name and file name

>>> import os
>>> os.path.split("E:\abc\efg.txt")   
('E:\abc', 'efg.txt')

os.path.splitext(path)

Simple string processing, split the path, return the tuple composed of its file name and extension

>>> import os
>>> os.path.splitext("E:\abc\efg.txt")   
('E:\abc\efg', '.txt')

os.path.join(dirpath,filename)

Concatenate the path with the file name, and automatically use the corresponding path separator according to the operating system. Windows uses “” and Linux uses “/”

>>> import os
>>> os.path.splitext("E:\abc\efg.txt")   
('E:\abc\efg', '.txt')

os.path.exists(path)

Determines whether the file or directory exists, and returns true or false

>>> import os
>>> os.path.exists("E:\abc\efg.txt")
False

os.path.isdir(path)

Judge whether the path is a folder or not. If the path does not exist, it returns false

>>> import os
>>> os.path.isdir("E:\abc\efg.txt")
False

os.path.isfile(path)

Determine whether the path is a file or not. If the path does not exist, it returns false

>>> import os
>>> os.path.isfile("E:\abc\efg.txt")
False

os.path.getsize(path)

Get the file size, unit bytes. If the file does not exist, an error will be reported. It cannot be used directly for a folder

>>> import os
>>> os.path.getsize("D:\2019-11-07.txt")
5973

os.path.getctime(path)

Get file creation time

os.path.getmtime(path)

Get the last modification time of the file

os.path.getatime(path)

Get the latest access time of the file

If you want to know more about programming and development, and grow with me, please pay attention to my official account, “pine nut warehouse”, and share the resources of the house and programmers. Thank you!!!

Recommended Today

Redis design and implementation 4: Dictionary Dict

In redis, the dictionary is the infrastructure. Redis database data, expiration time and hash type all take the dictionary as the underlying structure. Structure of dictionary Hashtable The implementation code of hash table is as follows:dict.h/dictht The dictionary of redis is implemented in the form of hash table. typedef struct dictht { //Hash table array, […]