Python Os.path Standard library Basics Learning 1

Source: Internet
Author: User

#文件系统标准库学习
Import Os,os.path
os.path# platform Standalone file/filename management
#以下给出一些常用的os模块下面使用的方法, as follows:
"""
1:os.sep--the delimiter between parts of the path, such as:/,\
2:OS.EXTSEP filename with file < extension > delimiter between, such as:.
The 3:os.pardir file path represents the first-level part of the directory tree, such as:.
The 4:os.curdir file path indicates the current directory part, such as:.
5:split () decomposes the function into 2 parts: Then a tuple, the last path of the tuple is the second element, the first
The element is everything before the last part.
The previous sections used strings, but these functions are not required (the path is real)
"""
#注意: It may be different from the example above in the standard library because it gives the path to my side without
#例1
def path (P):
P= (P)
For I in P:
print '%15s:%s '% (I,os.path.split (i))
#注意: Os.sep end, the last element of the path is an empty string
def path (P):
P= (P)
For I in P:
print '%15s:%s '% (I,os.path.basename (i))


#整个路径会剥除到只剩下最后一个元素, whether it is a file or a directory, if the path ends with a directory delimiter (OS.SEP), the base is considered empty.
#dirname () returns the first part of the path, if combined with basename () to get the original path
#dirname (PATH)
def path (P):
P= (P)
For I in P:
print '%15s:%s '% (I,os.path.dirname (i))



The #splitext () function is similar to split (), but it decomposes the path based on the extension delimiter instead of the directory delimiter
def path (P):
P= (P)
For I in P:
print '%15s:%s '% (I,os.path.splitext (i))



#查询扩展名时, only the last occurrence of os.extsep is used, and if a file name has multiple extensions, the partial extension is left on the prefix when the filename is decomposed

#commonprefix () takes a list of paths as a parameter, returns a string representing the common prefix that appears in all paths, which may represent a path that does not exist at all, and does not take into account the path delimiter, which may not fall on a delimiter boundary.
def path (P):
P= (P)
For I in P:
print '%15s:%s '% (I,os.path.commonprefix (i))


#建立路径
#结合路径: Use Join () #拿一个不存在的文件路径作为例子
def joins (PATH1,PATH2):
For Path3 in [(PATH1,PATH2)]:
Print Path3,os.path.join (*PATH3)

path1= ' d:\\a '
Path2= ' CC '
Joins (PATH1,PATH2)

#如果连续的某个参数以os. Sep begins, all parameters are discarded, and the new parameter becomes the starting part of the return value.

#处理 < variable > path: Auto-expand
For the user in [', ' admin ', ' accpet ', ' Password ']:
temp= ' ~ ' +user
print '%12s:%s '% (temp,os.path.expanduser (temp))
#如果用户主目录无法找到, the string does not make any changes directly back

#expandvars () is more general, it expands all shell environment variables that appear in the path

os.environ[' MYVAR ']= ' VALUE '
Print Os.path.expandvars ('/path/to/$MYVAR ')

Python Os.path Standard library Basics Learning 1

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.