os library of python standard library

The os library is a relatively important library in the python standard library

Its main functions are as follows:

1. System related variables and operations

2. File and directory related operations

3. Execute commands and manage processes

1. Commonly used function operations

1.os.name

The output string indicates the platform being used. If it is window, output 'nt', for Linux/Unix users, output 'posix'.

import os

dlj = os.name
print(dlj)

2.os.getcwd()

get the current working directory

dlj = os.getcwd()
print(dlj)

3.os.listdir(path)

Return all files and directory names under the specified directory path in the form of a list.

import os

path = './data'  # 自己随便指定一个路径
dlj = os.listdir(path)
print(dlj)

4.os.mkdir(path)

Create a new directory, path is a string indicating the path of the new directory. Important: This function can only generate the next-level directory, and cannot recursively generate deep directories, and if the folder already exists, an error will be reported.

import os

path = './wzh' 
os.mkdir(path) # 在工程路径下会生成一个文件夹,名称为wzh

5.os.makedirs(path)

Multiple levels of recursive directories can be generated. ( os.mkdir(path)comparison with)

import os

path = './wzh/aaa'
os.makedirs(path) # 在工程路径下会生成wzh文件夹,再在wzh文件夹下生成aaa文件夹。如果最后一级目录存在

6.os.rmdir(path)

Delete an empty directory, and report an error if the specified path is not an empty directory.

import os

path = './wzh/aaa'
os.makedirs(path)
os.rmdir('./wzh/aaa') # 删除wzh文件夹下的aaa文件夹,wzh文件夹未删除。

7.os.removedirs(path)
 

If the directory is empty, delete it, and recurse to the upper level directory, if it is also empty, delete it, and so on

import os

path = './aaa/bbb/ccc'
os.makedirs(path)  # 递归创建目录
os.removedirs(path) # 递归删除目录

8.os.remove(path)

Delete the specified file! ! ! ( os.rmdir(path)The difference os.rmdir(path)is to delete the specified empty folder, os.remove(path)but to delete the specified file)

import os

os.remove('./1.txt')  # 删除指定文件 这里删除.txt文件(文件是带有.后缀的)

9.os.sep

  1. Can override OS-specific path separators.
import os

path = './dlj' + os.sep + 'wzh'
print(path) # ./dlj\wzh 其中 os.sep 代替路径分割符
os.makedirs(path) # 递归生成指定路径

 10.os.linesep

Replaces the escape character \r\n.

import os

path1 = '/dlj\r\nwzh'
path2 = '/dlj' + os.linesep + 'wzh' # os.linesep 代表转移字符 \r\n
print(path1)
print(path2)
if path1 == path2:
    print('ok')

11.os.rename(path1,path2)

File rename.


import os

path1 = './0001.jpg'  # 已经存在的文件
path2 = './aaa.jpg'  #更改后的文件名!!!(文件名后缀要一致)
os.rename(path1, path2) # 把 path1文件 更改为 path2文件 path1文件就不存在了

12.the.curdir

return current path

13.the.deliver

Returns the parent directory of the current path, but it needs to be used in conjunction with os.path.abspath()

import os

print(os.curdir) # 返回'.'以表示当前路径
print(os.pardir) # 返回'..'以表示当前路径的父目录
# 这两个函数需要结合 os.path.abspath使用
aaa = os.path.abspath(os.curdir) 
bbb = os.path.abspath(os.pardir)
print(aaa)
print(bbb)

14.os.chdir(path)

Change the current working path

import os

path = 'D:/software/anaconda3/envs/torch1.2'
print(os.getcwd()) # 返回当前脚本工作路径
os.chdir(path) # 改变当前脚本工作路径
print(os.getcwd()) 

15.os.stat(path)

Get information about a file/path.

16.os.environ

Get the environment variables of the operating system

17.os.walk(path)

root: refers to the address of the folder currently being traversed
dirs: is a list, the content is the names of all directories in the folder (excluding subdirectories)
files: also a list, the content is the file All files (excluding subdirectories) in the folder
are generally combined with a for loop to traverse all files and directories under the directory.
 

import os

path = './result'

for root, dirs, files in os.walk(path):
    for item in dirs:
        print(os.path.join(root, item))
    for item in files:
        print(os.path.join(root, item))

2. The path module in the os library

We can manually implement the functions of the os.path module by using string operations. The function of this module is to allow us to realize the same function without having to consider the specific system, especially without paying too much attention to the issue of file system separators. It is a module that is used very frequently.

1.os.path.abspath(path)

Returns the normalized absolute path of path. Refer to os.pardirthe usage above.

2.os.path.join(path1,path2,...)

Return after combining multiple paths, parameters before the first absolute path will be ignored.

import os

dlj = os.path.join('./dlj', 'aaa', 'bbb')
print(dlj)

3.os.path.getatime(path)

Returns the last access time of the file or directory pointed to by path.

4.os.path.getmtime(path)

Returns the last modification time of the file or directory pointed to by path

5.os.path.getctime(path)

Returns the creation time of the file or directory pointed to by path

import os

path = './dlj6.py'
x = os.path.getatime(path) # 返回path所指向的文件或者目录的最后访问时间
y = os.path.getctime(path) # 返回path所指向的文件或者目录的创建时间
z = os.path.getmtime(path) # 返回path所指向的文件或者目录的最后修改时间
print(x)
print(y)
print(z)

6.os.path.basename(path)

Returns the last level filename or directory.

import os

path = './input/detection-results'
print(os.path.basename(path))

7.os.path.dirname(path)

Returns the file path (parent path)

import os

path = ',/input/detection-results/onecode_0a1d5cfbeb04312b01c634e534b78803.txt'
print(os.path.dirname(path))

8.os.path.exists(path)

Returns True if path exists; returns False if path does not exist.

9.os.path.getsize(path)

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

10.os.path.isabs(path)

Determine whether it is an absolute path.

import os

path = ',/input/detection-results/onecode_0a1d5cfbeb04312b01c634e534b78803.txt'
print(os.path.isabs(path))
path1 = os.getcwd()
path2 = os.path.join(path1, path)
print(os.path.isabs(path2))

11.os.path.isdir(path)

Determine whether the path is a directory.

12.os.path.isfile(path)

Determine whether the path is a file.

13.os.path.islink(path)

Determine whether the path is a link.

14.os.path.samefile(path)

Determine whether the paths are the same.

15.os.path.split(path)

Split the path into dirname and basename, returning a tuple.

import os

path = ',/input/detection-results/onecode_0a1d5cfbeb04312b01c634e534b78803.txt'
print(os.path.split(path))

16.os.path.splitext(path)

Split a path, returning a tuple of pathname and file extension.

17.os.path、realpath(path)

Returns the real path of path. (absolute path)

Guess you like

Origin blog.csdn.net/m0_74043494/article/details/128056794