python, os operation file, file path

Recently I saw the os module of python, as well as the specific usage of os and os.path. I don't understand it, so I will record it here.
Concept: python gets the upper-level directory of the file: get the upper-level directory of the directory where the file is located os.path.pardir
: is the parent directory,
os.path.abspath: is
the
The real absolute path is: D:\Python\test.py, now run the test.py file, python test.py. The test code is as follows:

import os ,os.path
print os.path.abspath("__file__")  # 获取当前文件的绝对路径
print os.path.dirname(os.path.abspath("__file__")) # 获取当前文件所在的目录名称
print os.path.pardir # 获取相对于文件当前目录的上级目录
print os.path.abspath(os.path.pardir)  # 获取相对于文件当前目录的上级目录的绝对路径
print os.path.join(os.path.dirname("__file__"),os.path.pardir) # 将文件的当前目录和文件当前目录的上级目录进行合并,取交集
print os.path.abspath(os.path.join(os.path.dirname("__file__"),os.path.pardir))

# 对应的输出为:
D:\Python\__file__
D:\Python
..   # “..”这是上级目录的表示方法
D:\
..  # 取 D:\Python\__file__ 和 D:\的交集,就是D:\,也就是"..",还是os.path.pardir的值。也就是文件当前目录的上一级(父级)目录。
D:\  # 也就是获取".."的绝对路径。

There are also several common commands:
The operation of files and folders (file operation functions) in python needs to involve the os module and the shutil module.
Get the current working directory, that is, the directory path where the current Python script works: os.getcwd()
Returns all files and directory names in the specified directory: The os.listdir()
function is used to delete a file: os.remove()
Delete multiple Directory: os.removedirs(r"c:\python")
to check whether the given path is a file: os.path.isfile()
to check whether the given path is a directory: os.path.isdir() to check
whether is an absolute path: os.path.isabs()
checks whether the given path actually exists: os.path.exists()
returns the directory name and file name of a path: os.path.split() eg os.path.split ('/home/swaroop/byte/code/poem.txt') result: ('/home/swaroop/byte/code', 'poem.txt')

Split extension: os.path.splitext()
Get pathname: os.path.dirname()
Get filename: os.path.basename()
Run shell commands: os.system()
Read and set environment variables: os .getenv() and os.putenv()
give the line terminator used by the current platform: os.linesep Windows uses '\r\n', Linux uses '\n' and Mac uses '\r'
to indicate which one you are using Platform: os.name For Windows it is 'nt' and for Linux/Unix users it is 'posix'
Rename: os.rename(old, new)
Create multi-level directories: os.makedirs(r"c: \python\test")
Create a single directory: os.mkdir("test")
Get file attributes: os.stat(file)
Modify file permissions with timestamp: os.chmod(file)
Terminate current process: os.exit()
Get file size: os.path.getsize(filename)

Reference URL: http://blog.csdn.net/longshenlmj/article/details/13294871

The following is a combination of specific codes to explain their application:

import os,os.path as osp
def check_dir(path):  
    """ make sure the dir specified by path got created """
    d = osp.abspath(osp.join(path, osp.pardir))
    if not osp.exists(d):
        os.makedirs(d)

# 代码调用:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325981145&siteId=291194637