Commonly used functions and methods
In Python, theos
The library provides a large number of functions and methods related to the operating system.
Below are some of the commonly used functions and methods along with their detailed descriptions, examples and full comments:
The getcwd() function:
Get the current working directory.
import os # Get current working directory current_dir = () print(current_dir)
In the code above, we usegetcwd()
function gets the current working directory and assigns it to the variablecurrent_dir
. This function returns a string representing the path to the current working directory.
listdir() function:
List all files and subdirectories in the specified directory
import os # List all files and subdirectories in the current working directory files = ('.') for file in files: print(file)
In the code above, we uselistdir()
function lists all files and subdirectories in the current working directory and assigns them to the variablefiles
. Then, we use thefor
The loop iterates over all files and subdirectories and prints their names.
-
mkdir()
Method: Create a new catalog.
import os # Create a new directory named 'new_dir' in the current working directory new_dir_path = ((), 'new_dir') (new_dir_path)
In the code above, we usemkdir()
method creates a new directory named 'new_dir' in the current working directory. Note that if the directory already exists, an exception is thrown.
-
rmdir()
Method: Delete the specified directory.
import os # Delete the specified directory dir_path = ((), 'new_dir') (dir_path)
In the code above, we usermdir()
method deletes the previously created 'new_dir' directory. It is important to note that when performing file or directory operations, you should always consider how to handle exceptions and take care to check permissions to ensure that the operation is legitimate.
-
rename()
Method: Rename the file or move the file to a new location.
import os # Move files from the current working directory to a new directory and rename them file_path = ((), '') new_file_path = ((), 'new_dir', 'new_example.txt') (file_path, new_file_path)
In the code above, we userename()
method moves the previously created '' file to the new directory 'new_dir' and renames it to 'new_example.txt'. If the target file already exists, it will be overwritten.
-
remove()
Method: Delete the specified file.
import os # Delete the specified file file_path = ((), 'new_dir', 'new_example.txt') (file_path)
In the code above, we useremove()
method deletes the 'new_example.txt' file that was just created.
() function:
- Checks if the specified path exists.
import os # Check if the file exists file_path = ((), '') if (file_path): print('Document exists') else: print('File does not exist')
In the code above, we useexists()
The function checks whether the ''file exists''. If the file exists, it prints out 'File exists', otherwise it prints out 'File does not exist'.
() function:
Checks if the specified path is a directory.
import os # Determine if the specified path is a directory dir_path = ((), 'new_dir') if (dir_path): print('is a directory') else: print('Not a directory')
In the code above, we useisdir()
The function determines if the path 'new_dir' is a directory. If it is, it prints 'is a directory', otherwise it prints 'is not a directory'.
() function:
- Checks if the specified path is a file.
import os # Determine if the specified path is a file file_path = ((), '') if (file_path): print('It's a file') else: print('Not a file')
In the code above, we useisfile()
The function determines whether the path '' is a file. If it is, it prints out 'is a file', otherwise it prints out 'is not a file'.
() function:
- Splice Path.
import os # Splice multiple parts into a single path path = ((), 'new_dir', 'new_example.txt') print(path)
In the code above, we usejoin()
function stitches together the current working directory, the new directory name, and the new filename into a complete path and prints it.
() function:
- Gets the filename or last level directory name in the path.
import os # Get the filename or last directory name in the path file_path = ((), 'new_dir', 'new_example.txt') dir_name = ((file_path)) file_name = (file_path) print(dir_name) print(file_name)
In the code above, we usedirname()
function to get the name of the directory where the 'new_example.txt' file is located, then use thebasename()
function gets the last level of the directory name (i.e. 'new_dir') and assigns it to the variabledir_name
. Next, we use thebasename()
function gets the file name 'new_example.txt' and assigns it to the variablefile_name
. Finally, we print out the values of the two variables.
Above.os
Some of the commonly used functions and methods in the library with their detailed descriptions, examples and full comments. There are also many other useful functions and methods available, which can be selected and used according to actual needs.
For more information on python os library methods follow my other related articles!