In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains how to use os.path in detail about the principle and usage of the python standard library. The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use os.path in detail about the principle and usage of the python standard library.
Path in os
If you look at the source code, you can see that there are several lines in os.py
If 'posix' in _ names: name =' posix' linesep ='\ n' from posix import * # omit some codes elif 'nt' in _ names: from nt import * try: from nt import _ exit _ all__.append (' _ exit') except ImportError: pass import ntpath as path #.
Since we are in windows (WindowsNT), we can open the ntpath.py file and see that eight member variables are defined at the beginning
Curdir ='.'# current path ID pardir ='..'# extsep ='.'# extension delimiter sep ='\\ 'pathsep =';'# path delimiter altsep ='/'# defpath ='.; C:\\ bin' # location of the environment variable devnull = 'nul'
In addition, there is a Boolean member variable called supports_unicode_filenames that indicates whether the file name of unicode is supported by the current system.
It can be understood that path encapsulates some features that are related to the operating system and have different codes under different operating systems. The default is from os.path import *.
A single parameter function that is input as a path string
Output normpath () modifies the path to\\ a\ b form normcase () modifies the path to lowercase\\ a\\ b form abspath () returns the absolute path in the format X:\\ a\\ brealpath () returns the absolute path and eliminates the link relpath () to return the relative path, and eliminates the link split () to divide the input path into two parts
For example, X:\\ a\\ b will return the extension splidrive () of the split file X:\ an and bsplitext () to split the directory or file name at the lowest level of the drive and other paths basename (), that is, the lowest directory or file name of split () [1] dirname (), that is, split () [0] exists () determines whether the input path exists, and returns True if it exists, otherwise Falselexists () if the path is damaged. Also return Falseisabs () to determine whether the input path is an absolute path isfile () to determine whether the input path is a file isdir () to determine whether the input path is a directory islink () to determine whether the input path is a link ismount () to determine whether the input path is a mount point (disk letter in windows)
For example, os.path.ismount ('C:\\') is returned as Trueexpanduser () to extend the path to a variable recognized by the command line by extending the path through ~ expandvars ()
Some examples
> from os.path import * > p = abspath ('.') > > pendant:\\ Documents\\ 00\\ 1022' > exists (p) True > splitdrive (p) ('Eleuza,'\\ Documents\ 00\\ 1022') > > isfile (p) the one-parameter function related to file information
Its input must be a file path, not a directory
Output ~ .getsize () gets the file size in bytes ~ .getctime () gets the file creation time, c means create~.getmtime () gets the last modification time of the file, m means modify~.getatime () gets the last access time of the file, and a means access
Output ~ .getsize () gets the file size in bytes ~ .getctime () gets the file creation time, c means create~.getmtime () gets the last modification time of the file, m means modify~.getatime () gets the last access time of the file, and a means the access input as a function with multiple parameters
Os.path.join, for stitching paths, is a super sweet feature. Many beginners will be bothered by creating new folders, and most of the time they don't know whether to write D:\ test or D:\ test\, which are basically equivalent in os.path.join.
> os.path.join ('test','\\ test1','test2\', 'test3')'\\ test1\\ test2\\ test3'
Determine whether it is the same ~ .samefile (p1, p2) determine whether the directory or file is the same ~ .sameopenfile (fp1, fp2) whether the two open files point to the same file
Commonpath (list), commonprefix (list): returns the longest path shared by all path in list, that is, the common parent folder from all files and folders. The difference between the two is that the latter adds\ to the return path.
Thank you for your reading, the above is the content of "detailed explanation of the principle and usage of python standard library how to use os.path". After the study of this article, I believe you have a deeper understanding of how to use os.path in the detailed explanation of the principle and usage of python standard library, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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: 0
*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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.