Python standard library-os.path and Glob learning

Source: Internet
Author: User
Tags glob

Reference blog:
Http://www.cnblogs.com/vamei/archive/2012/09/05/2671198.html
Http://www.cnblogs.com/baiqi/p/3951506.html
Http://www.cnblogs.com/sunnyjiangjie/p/4207063.html
Reference: Python 2.7.7 Documentation
Reference tool: http://translate.google.cn/
Set path = '/home/vamei/doc/file.txt '
Note that the results under WinDOS and Linux may be different

One, Os.path (common path operation)
Os.path.abspath (path) returns the absolute path normalized by path.
WinDOS
>>> Os.path.abspath (PATH)
' C:\\home\\vamei\\doc\\file.txt '
Linux:
>>> Os.path.abspath (PATH)
'/home/vamei/doc/file.txt '
os.path.basename (path)File names that are included under query path
>>> os.path.basename (PATH)
' File.txt '
The Os.path.commonprefix (list) returns the longest path common to all paths in the list. Note For the second example, the return may not be a path.
>>> os.path.commonprefix ([path, '/home/', '/home/he.txt ')
'/home/'
>>> os.path.commonprefix ([' asdf1234 ', ' asdf50548 '])
' Asdf '
os.path.dirname (path)Return path name
os.path.exists (path)True if path exists, or if path does not exist or does not have permission to execute Os.stat (), or if the corrupted link returns false
Read other blog explanations, links or not verified
┗os.path.lexists (path) differs from os.path.exists (path) If there is a broken link that returns True
Os.path.expanduser (path) converts the "~" and "~user" contained in path to the user directory; Read the Python manual.
Os.path.expandvars (PATH) replaces the $name and ${name} contained in path based on the value of the environment variable name
Os.path.getatime (Path) returns the last time it entered this path.
os.path.getmtime (path)Returns the time of the last modification of this path
Os.path.getctime (path) returns CTime, (class Unix) time of the last change; Windows, was the creation time for path
os.path.getsize (path)Returns the path size, in units of byte
os.path.isabs (path)Determine if absolute path
os.path.isfile (path)Determine if the path is a regular file, and note that the existence is determined first
os.path.isdir (path)To determine if the path is an existing directory, note that the existence is first judged
os.path.islink (path)Determine if the path is link
os.path.ismount (path)Determine if the path is a mount point
Os.path.join (path1[, path2[, ...])Use the directory name and file name to form a path string that automatically uses the delimiter Os.sep
>>> os.path.join (' c:\\ ', ' mydoc ', ' xxx ', ' my.txt ')
' C:\\mydoc\\xxx\\my.txt '
Os.path.normcase (path) converts the case and slash of path
Os.path.normpath (path) canonical path string form
Os.path.realpath (PATH)
Os.path.relpath (path[, start]) calculates the relative path from start
Os.path.samefile (path1, path2) determine if the directory or file is the same
Os.path.sameopenfile (FP1, FP2) determine if FP1 and FP2 point to the same file
Os.path.samestat (STAT1, STAT2) determines whether the stat tuple STAT1 and Stat2 point to the same file
os.path.split (path)Splits the path into dirname and basename, returning a tuple
Os.path.splitdrive (path) returns a tuple that consists of a drive name and path
>>> os.path.splitdrive (' C:\\mydoc\\my.txt ')
(' C: ', ' \\mydoc\\my.txt ')
Os.path.splitext (path) split path, returns a tuple of pathname and file name extension
>>> Os.path.splitext (PATH)
('/home/vamei/doc/file ', '. txt ')
Os.path.splitunc (path) splits the path into the load point and file
Os.path.walk (path, visit, ARG) iterates through the path, entering each directory calls the visit function, and the visit function must have
3 parameters (ARG, dirname, names), dirname represents the directory name of the current directory,
Names represents all filenames under the current directory, and Args is the third parameter of walk
Os.path.supports_unicode_filenames Set whether Unicode pathname is supported

Ii. glob (Unix style pathname pattern expansion)
Wildcard operation only supported *,?, [] These three wildcard characters
Glob.glob (pathname) lists all files that match the expression
>>> glob.glob (' d:\email\* ')
[' D:\\email\\archive 2013-11.pst ', ' d:\\email\\outlook.ost ', ' D:\\email\\~outloo
K.ost.tmp ']
Glob.iglob (pathname) gets a Glob.glob (pathname) object that can be traversed

Python standard library-os.path and Glob learning

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.