SoFunction
Updated on 2024-12-19

Python learning os module and its usage

The os module represents the operating system where the program is running, and is mainly used to get information about the operating system where the program is running.

You can see all the attributes and functions contained in the module by importing the os module into Python's interactive interpreter and typing the os.__all__ command (the __all__ variable represents the module's open public interface).

This section describes only the properties and functions that are commonly used in the os module:

  • : Returns the name of the operating system on which the imported dependency module is running, usually one of 'posix', 'nt', 'java', etc.
  • : Returns a dictionary of all environment variables on the current system.
  • (filename): This function encodes the filename of a path-like (path) file.
  • (filename): This function decodes a path-like filename.
  • : This is a class representing a path-like object.
  • (key, default=None): Get the value of the specified environment variable.
  • (): Returns the current system login user name. Corresponding to this function are the functions (), (), (), etc., which are used to obtain user IDs, user groups, group IDs, etc., and which are usually valid only on UNIX systems.
  • (): Get the current process ID.
  • (): Get the parent process ID of the current process.
  • (key, value): this function is used to set the environment variable.
  • os.cpu_count(): return the current system CPU count.
  • : Returns the path separator.
  • : Returns the separator between multiple paths on the current system. Generally, the separator between multiple paths on Windows is a semicolon (;); on UNIX and UNIX-like systems (e.g. Linux, Mac os X), the separator between multiple paths is a colon (:).
  • : Returns the line break of the current system. Generally, on Windows system, the line break is "\r\n": on UNIX system, the line break is "\n"; on Mac os X system, the line break is "\r ".
  • (size): Returns an object of up to N bytes suitable for encryption. This function returns random bytes from an operating system-specific source of randomness, which is usually unpredictable and therefore suitable for most encryption scenarios.

The following program demonstrates the usage of most of the functions of the os module:

import os
# Display the name of the operating system into which the dependent module is imported
print()
# Get the value of the PYTHONPATH environment variable
print(('PYTHONPATH'))
# Returns the current system login user name
print(())
# Get the current process ID
print(())
# Get the parent process ID of the current process
print(())
# Returns the number of CPUs in the current system
print(os.cpu_count())
# Return path separator
print()
# Returns the path separator for the current system
print()
# Returns the line breaks of the current system
print()
# Returns bytes of up to 3 bytes suitable for encryption.
print((3))

Run the above program to see the following output:

nt
None
yeeku
9904
12036
8
\
;

b'\x12\x1e\xcf'

From the output above, we can see that on Windows, the operating system name for Python to import the dependency module is "nt"; the login user name of the current system is "yeeku"; the current process ID is "9904"; the parent process ID of the current process is "12036"; there are 8 CPUs on the current system; the path separator of the current system (Windows) is "\". 9904"; the parent process ID of the current process is "12036"; there are 8 CPUs on the current system; the path separator of the current system (Windows) is "\"; the path separator of the current system (Windows) is "\"; and the path separator of the current system (Windows) is "\". "; the separator between multiple paths on the current system (Windows) is a semicolon (;); however, the line breaks are not clearly visible on the current system (Windows) because two blank lines are generated only when "\r\n" is output on the console.

In addition, the os module contains a large number of functions for manipulating files and directories, which will be covered in subsequent chapters of this tutorial.

The os module also contains a variety of process management functions that can be used to start new processes, abort existing processes, and so on. The process management related functions in the os module are as follows:

(): Generates a SIGABRT signal to the current process. On UNIX systems, the default behavior is to generate a kernel dump; on Windows systems, the process returns immediately to exit code 3.

(path, arg0, arg1, ...) : This function has a series of similar functions, such as (), (), etc., which use the argument list arg0, arg1, ... to execute the execution file represented by path. These functions all use the argument list arg0, arg1,... to execute the file represented by path.

(): fork a child process.

(pid, sig): send the sig signal to the process corresponding to pid to end the process.

(pgid, sig): send the sig signal to the process group corresponding to pgid.

(cmd, mode='r', buffering=-1): Used to open a read/write pipeline to the cmd command (read-only pipeline when mode is r, read/write pipeline when mode is rw), the buffering buffer argument has the same meaning as the built-in open() function. The file object returned by this function is used for reading and writing strings, not bytes.

(mode, path, ...) : This function has a series of functions with similar functionality, such as (), (), etc., which are used to execute a new program in a new process.

(path[,operation]): Execute the operation corresponding to the specified file using the tool associated with the file. If operation is not specified, the default operation is open. operation must be a valid command line operation, such as open, edit, print, etc. The operation parameter must be a valid command line operation.

(command): Runs the specified command on the operating system.

The following program demonstrates the functionality of the functions related to process management in the os module:

import os
# Run the cmd command on the platform
('cmd')
# Use Excel to open g:\ files
('g:\\')
(os.P_NOWAIT, 'E:\\\Tools\\\\Editing Tools\\\\\Notepad++.7.5..x64\\\\notepad++.exe', ' ')
# Execute the os_test.py program using the python command
("D:\\Python\\Python36\\", " ", 'os_test.py', 'i')

If you run the above program directly, you can see that after running the program, it opens the file using Excel, opens the Notepad++ tool, and runs the os_test.py file using the python command. However, if you uncomment the bolded code in the program, you will see that the program just starts the cmd command line program, because when you use the () function to run the program, the process of the new program replaces the existing process.

Knowledge Points Supplement:

os stands for "operating system", and as the name implies, the os module provides an interface for various Python programs to interact with the operating system. Using the os module makes it easy to interact with the operating system on the one hand, and greatly enhances the portability of your code on the other. If something goes wrong with a function in this module, an OSError exception or its subclasses will be thrown.

take note of

If you are reading or writing files, it is recommended to use the built-in function open();

For path-related operations, it is recommended to use the os submodule;

If you want to read multiple files line by line, it is recommended to use the fileinput module;

To create temporary files or paths, it is recommended to use the tempfile module;

For more advanced file and path manipulation the shutil module should be used.

to this article on the Python learning os module and the use of the article is introduced to this, more related Python os module and the use of content, please search for my previous articles or continue to browse the following related articles I hope that you will support me in the future more!