Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of the os.path () module

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the use of the os.path () module, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

The os.path module is mainly used to obtain the attributes of a file.

Abspath () method

Returns the absolute path to a directory.

Syntax: os.path.abspath (path) example: import os pat = os.path.abspath ('new') print (pat) # C:\ Users\ lu\ PycharmProjects\ test_project\ content\ newbasename () method

Returns the file name of a directory.

Syntax: os.path.basename (path) example: import osdir = os.path.basename ('/ test_project/content/new') print (dir) # newdirname () method

Returns the file path of a directory.

Syntax: os.path.dirname (path) example: import os dir = os.path.dirname ('/ test_project/content/new') print (dir) # / test_project/contentjoin () method

Combine the directory and file name into one path. Windows uses\ stitching by default.

Syntax: os.path.join (path2 [, path3 [,...]]) Example: import osdir = os.path.join ('/ test_project/content', 'new') print (dir) # / test_project/content/newsplit () method

Split the path into dirname and basename and return a tuple.

Syntax: os.path.split (path) example: import osresult = os.path.split ('/ test_project/content') print (result) # ('/ test_project', 'content') exists () method

Tests whether the specified file exists. Returns True if it exists, and False if it doesn't exist.

Syntax: os.path.exists (path) example:

Suppose the file xkd.py exists, as follows:

Import osresult = os.path.exists ('xkd.py') print (result) # True# there is no method to return Falseresult1 = os.path.exists (' aa.py') print (result1) # Falsegetatime ()

Gets the last access time of the specified file. Returns the number of floating-point seconds.

Syntax: os.path.getatime (path) example:

Output the last access time of the xkd.py file, as follows:

Import osresult = os.path.getatime ('xkd.py') print (result) # 1565667299.7847126getctime () method

Gets the last change time of the specified file. Returns the number of floating-point seconds.

Syntax: os.path.getctime (path) example:

The last change time of the output test.py file is as follows:

Import osresult = os.path.getctime ('test.py') print (result) # 1565853902.035506getmtime () method

Gets the last modification time of the specified file.

Syntax: os.path.getmtime (path) example:

Output the last modification time of the test.py file, as shown below:

Import osresult = os.path.getmtime ('test.py') print (result) # 1565861158.5235534getsize () method

Returns the file size, or an error if the file does not exist.

Syntax: os.path.getsize (path) example:

The size of the output test.py, all as follows:

Import osresult = os.path.getsize ('test.py') print (result) # 85result1 = os.path.getsize (' aa.py') print (result1) # error: FileNotFoundErrorcommonprefix () method

Returns the longest path prefix (character by character), which is the prefix for all paths in the list. If list is empty, an empty string ('') is returned.

Syntax: os.path.commonprefix (list) example: import osresult = os.path.commonprefix (['content',' content/xkd', 'content/xkd/test.py']) print (result) # contentsplitext () method

Splits the path and returns a tuple of the pathname and file extension.

Syntax: os.path.splitext (path) example: import osresult = os.path.splitext ("/ content/xkd.py") print (result) # ('/ content/xkd', '.py') lexists () method

Determines whether the path exists, returns True if the path exists, and returns True if the path is a corrupted soft link (the original file / folder was deleted).

Syntax: os.path.lexists (path) example: import osresult = os.path.lexists ('C _

Convert the ~ and ~ user contained in path into user directories.

Syntax: os.path.expanduser (path) example: import osresult = os.path.expanduser ('~ / test_project/content') print (result) # C:/Users/lu/test_project/contentresult1 = os.path.expanduser ('~ test_project/content') print (result1) # C:/Users/test_project/contentexpandvars () method

Extend the environment variable in path and return. The substrings in the format $NAME and ${NAME} in path will be replaced by the corresponding environment variables (there is a case-sensitivity problem on different platforms). If the format is incorrect or the variable does not exist, it will be returned directly.

The form of% NAME% is also included in windows.

Syntax: os.path.expandvars (path) isabs () method

Determine whether it is an absolute path.

Syntax: os.path.isabs (path) example: import osresult = os.path.isabs ("new") print (result) # Falseisabs () method

Determine whether it is an absolute path. True is returned if yes, False is returned otherwise.

Syntax: os.path.isabs (path) example: import osresult = os.path.isabs ("/ test_project/content") print (result) # Trueresult1 = os.path.isabs ("new") print (result1) # Falseisfile () method

Determine whether the path is a file.

Syntax: os.path.isfile (path) example: import osresult = os.path.isfile ("test.py") print (result) # True# is not a file return Falseresult1 = os.path.isfile ("new") print (result1) # Falseislink () method

Determine whether the path is a link.

Syntax: os.path.islink (path) example: import osresult = os.path.isdir ("/ test_project/content") print (result) # Falseismount () method

Determine whether the path is a mount point.

Syntax: os.path.ismount (path) example: import osresult = os.path.ismount ("/ content/new") print (result) # Falsenormcase () method

Standardize the case of pathnames. On Unix and Mac OS X systems, return path directly. In systems that are case-insensitive, such as Windows, path is converted to lowercase letters. In addition, Windows converts / to\\.

Syntax: os.path.normcase (path) example: import osresult = os.path.normcase ('/ CONTENT/xkd.py') print (result) #\ content\ xkd.pynormpath () method

Standardize the path string form.

Syntax: os.path.normpath (path) example: import osresult = os.path.normpath ('/ content/xkd.py') print (result) #\ content\ xkd.pyrealpath () method

Returns the real path to path.

Syntax: os.path.realpath (path) example: import osresult = os.path.realpath ("/ content/new") print (result) # C:\ content\ newrelpath () method

Returns the relative path relative to the current working directory or the specified start directory.

Syntax: os.path.relpath (path, start=os.curdir) example: import osresult = os.path.relpath ('/ test_project/content', start=os.curdir) print (result) #..\ test_project\ contentsamefile () method

Determining whether path2 and path3 point to the same file or directory depends on the device number and i-node. If the call to os.stat () fails, an exception will be thrown.

Syntax: os.path.samefile (path2, path3) example: import osresult = os.path.samefile ('test.py',' xkd.py') print (result) # Falsesameopenfile () method

Determine whether the file descriptor FP1 and fp2 point to the same file or directory.

Syntax: os.path.sameopenfile (fp1, fp2) example: import osfd1 = os.open ("test.txt", os.O_RDWR | os.O_CREAT) fd2 = os.open ("test.txt", os.O_RDWR | os.O_CREAT) fd3 = os.open ("xkd.txt", os.O_RDWR | os.O_CREAT) result = os.path.sameopenfile (fd1, fd2) print (result) # Trueresult1 = os.path.sameopenfile (fd1, fd3) print (result1) # Falsesamestat () method

Determine whether the status tuples stat1 and stat2 point to the same file or directory, and the parameter may be the return value of os.fstat (), os.lstat, or os.stat ().

Syntax: os.path.samestat (stat1, stat2) example: import osstat1 = os.stat ("test.py") stat2 = os.stat ("test.py") stat3 = os.stat ("xkd.py") result = os.path.samestat (stat1, stat2) print (result) # Trueresult1 = os.path.samestat (stat1, stat3) print (result1) # Falsesplitdrive () method

It is generally used under windows to return a tuple of drive name and path.

Syntax: os.path.splitdrive (path) example: import osresult = os.path.splitdrive ('/ test_project/content/xkd.py') print (result) # (', / test_project/content/xkd.py') supports_unicode_filenames

Determine whether any Unicode string can be used as the file name (within the restrictions imposed by the file system).

Thank you for reading this article carefully. I hope the article "what is the use of os.path () module" shared by the editor will be helpful to everyone? at the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 260

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report