May 30, 2021 Article blog
os module: is a call to the operating system that simulates instructions to the operating system
The os module provides a portable way to use operating system-related features. If you only want to read or write files, use the open() method, the os.path module if you want to manipulate the path, the fileinput module if you want to read all the lines in all files on the command line, use the tempfile module for information about creating temporary files and directories, and the shutil module for advanced file and directory processing.
The following is an introduction to the features that are often used;
1. Get the current path and the files under the path
os.getcwd(): View the current path.
os.listdir (path): List all the files in the directory.
The list type is returned.
import os
CP = os.getcwd () # Get the current path, return an absolute path
print(cp) # C:\Users\Desktop\w3cschool\os
FileList = OS.ListDir (CP) # Gets files under the path, as well as folders
print(fileList) # ['os_.py', 'sub', 'test.txt']
2. Gets the absolute path of the path
os.path.path.path: Returns the absolute path of the path
Abspath1 = OS.Path.Abspath (".") # Returns the absolute path of the current path
print(abspath1) # C:\Users\Desktop\w3cschool\os
Abspath2 = os.path.abspath ("../") # Returns the absolute path of the previous layer path
print(abspath2) # C:\Users\Desktop\w3cschool
os.path.split: Breaks the path down into (folders, file names) and returns the tuple type. A s you can see, if the last character of the path string is, only the folder portion has a value, and if none of the path strings are, only the file name part has a value. I f the path string has a value and is not in the end, both the folder and the file name have values. And the results of the returned folder do not contain .
os.path.join (path1, path2,...): Combine paths, and if there is an absolute path, the previous path is deleted.
catalogue_and_file1 = os.path.split('D:\pythontest\ostest\Hello.py')
print(catalogue_and_file1) # ('D:\\pythontest\\ostest', 'Hello.py')
catalogue_and_file2 = os.path.split('.')
print(catalogue_and_file2) # ('', '.')
catalogue_and_file3 = os.path.split('D:\pythontest\ostest\\')
print(catalogue_and_file3) # ('D:\\pythontest\\ostest', '')
path_join1 = os.path.join('D:\pythontest', 'ostest')
print(path_join1) # D:\pythontest\ostest
path_join2 = os.path.join('D:\pythontest\ostest', 'hello.py')
print(path_join2) # D:\pythontest\ostest\hello.py
path_join3 = os.path.join('D:\pythontest\\b', 'D:\pythontest\\a')
print(path_join3) # D:\pythontest\a
os.path.dirname (path): Returns the folder portion of the path with no '';
os.path.basename: Returns the file name in the path.
4.os.path module details
#Obtain
Os.path.abspath () # Get absolute paths of a file
Os.path.getime () # Returns the last access time, timestamp of the file or directory referred to PATH.
Os.path.getmtime () # Returns the final modification of the file or directory referred to PATH, timestamp
# The following three can not consider the path
Os.path.split ('C: \ A \ TES.TXT') # Returns a tuple, two parts, a directory, a file name
Os.path.Dirname (R'c: \ a \ tex.txt ') # Get the absolute path of the file
Os.Path.BaseName (R'c: \ a \ ts.txt ') # Get the last value
# 是 存
Os.path.exists (R'c: ') # 路 路 是
Os.path.isabs (R'c: \ a ') # Judging whether an absolute path
Os.path.isfile (R'c: \ a \ TES.TXT ') # 否 文件
Os.path.ISDIR () # is a folder
# Combine multiple parts into a path
Os.path.join (R'c: ', R' \ B ', R' \ A.TXT ') # 将 多 多 组 组
Recommended lessons: Python3 Getting Started, Python3 Advanced