Common methods of OS modules

Keywords: Programming Python

os module provides the function interface function of most operating systems. when os module is imported, it will adapt to different operating system platforms and operate according to different platforms.

getcwd() method

The getcwd() method is used to return the working directory of the current process.

Syntax: os.getcwd()

Examples:

import os
print(os.getcwd())

# C:\Users\lu\testfile

listdir() method

The listdir() method is used to return a list of files or folder names contained in the specified folder. The list is in alphabetical order.

Syntax: os.listdir(path)

Path is the directory path that needs to be listed

Examples:

import os

path = "C:/Users/lu/xxx"  # Route
lst = os.listdir(path)

# Output all files and folders
for i in lst:
    print(i)

'''
.idea
blog
haha
manage.py
templates
testfile
venv
'''

remove() method

The remove() method is used to delete files on a specified path. If the specified path is a directory, an OSError exception is thrown. This method has no return value.

Grammar: os.remove(path)

Path is the file path to be removed

Examples:

import os
# Look at all the files in the directory first
print(os.listdir(os.getcwd()))  # ['examination.xls', 'test.py', 'test.txt']

# Remove the specified file
os.remove("test.txt")

# View the removed directory
print(os.listdir(os.getcwd()))  # ['examination.xls', 'test.py']

rmdir() method

The rmdir() method is used to delete the directory of the specified path. The folder can be deleted only if it is empty, otherwise an OSError exception is thrown.

os.rmdir(path)

path Directory path to delete

Examples:

import os
# View all files in the directory
print(os.listdir(os.getcwd()))  # ['directory', 'examination.xls', 'test.py', 'test.txt']

# Remove the specified directory
# os.rmdir('directory')

# Only directories can be removed. If files are removed, errors will be reported.
os.rmdir('test.txt')

# View the removed directory
print(os.listdir(os.getcwd()))  # ['examination.xls', 'test.py', 'test.txt']

removedirs() method

The removedirs() method is used to delete empty directories that are multilevel recursive. If there are files in the directory, they cannot be deleted.

os.removedirs(path)

path Directory path to remove

Examples:

import os
# View all files in the directory
print(os.listdir(os.getcwd()))  # ['mki','test.py', 'test.txt']

# Remove the specified directory
os.removedirs('mki')

# View the removed directory
print(os.listdir(os.getcwd()))  # ['test.py', 'test.txt']

mkdir() method

The mkdir() method is used to create directories in digital permission mode. The default mode is 0777 (octal).

Syntax: os.mkdir(path[, mode])

path is the directory to be created
 The permission numeric mode to set for the directory by mode

Examples:

import os

# Create directories
path = 'C:/Users/lu/PycharmProjects/haha/testfile/mki'

os.mkdir(path)

print("Directory created")

makedirs() method

The makedirs() method is used to create directories recursively, similar to the mkdir() method, but all intermediate-level folders created need to contain subdirectories.

Syntax: os.makedirs(path, mode=0o777)

Directories that path needs to be created recursively
 Mode permission mode

Examples:

import os

path = "C:/Users/lu/PycharmProjects/haha/eefile/aa"
os.makedirs(path, 0o777)
print ("Paths are created")

chdir() method

The chdir() method is used to change the current working directory to the specified path.

Syntax: os.chdir(path)

Path is the new path to switch to

Examples:

import os

# View the current working directory
print(os.getcwd())  # C:\Users\lu\PycharmProjects\haha\eefile

# New path to switch to
path = 'C:/Users/lu/PycharmProjects/haha/haha'

# Modify the current working directory
os.chdir(path)

# View the modified working directory
print(os.getcwd()) # C:\Users\lu\PycharmProjects\haha\haha

Reference resources: https://docs.python.org/3/library/os.html?highlight=os

Reference resources: https://www.9xkd.com/

Posted by yoki on Thu, 10 Oct 2019 00:34:41 -0700