Python learning os module and usage

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

Import the os module in the Python interactive interpreter, and then enter the os.all command (the all variable represents the public interface of the module), and you can see all the attributes and functions contained in the module.

Here only introduces the commonly used attributes and functions in the os module:

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

import os
# Display the name of the operating system importing dependent modules
print(os.name)
# Get the value of the PYTHONPATH environment variable
print(os.getenv('PYTHONPATH'))
# Returns the login user name of the current system
print(os.getlogin())
# Get the current process ID
print(os.getpid())
# Get the parent process ID of the current process
print(os.getppid())
# Returns the number of CPUs in the current system
print(os.cpu_count())
# Return path separator
print(os.sep)
# Returns the path separator of the current system
print(os.pathsep)
# Returns the newline character of the current system
print(os.linesep)
# Return bytes of up to 3 bytes suitable for encryption
print(os.urandom(3))

Run the above program, you can see the following output:

nt
None
yeeku
9904
12036
8

;
b’\x12\x1e\xcf’

It can be seen from the above output that the name of the operating system that Python imports dependent modules on the Windows system is "nt"; the login user name of the current system is "yeeku": the current process ID is "9904"; the parent process of the current process The ID is "12036"; there are 8 CPUs on the current system; the path separator of the current system (Windows) is ""; the separator between multiple paths on the current system (Windows) is a semicolon (;); but On the current system (Windows), the newline character cannot be seen obviously, this is because two blank lines will be generated when "\r\n" is output in the console.

In addition, the os module also contains a large number of functions for manipulating files and directories. This tutorial will specifically introduce these functions in subsequent chapters.

The os module also contains various process management functions, which can be used to start new processes, terminate existing processes, and so on. The functions related to process management under the os module are as follows:

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

os.execl(path, arg0, arg1, …): This function also has a series of similar functions, such as os.execle(), os.execlp(), etc. These functions all use parameter lists arg0, arg1,... Executing the file represented by path.

os.forkpty(): fork a child process.

os.kill(pid, sig): Send the sig signal to the process corresponding to pid to end the process.

os.killpg(pgid, sig): Send the sig signal to the process group corresponding to pgid.

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

os.spawnl(mode, path, …): This function also has a series of functions with similar functions, such as os.spawnle(), os.spawnlp(), etc. These functions are used to execute new programs in new processes.

os.startfile(path[,operation]): Use the tool associated with the file to perform the operation corresponding to the specified file. If you do not specify an operation operation, the open operation is performed by default. The operation parameter must be a valid command line operation item, such as open (open), edit (edit), print (print), etc.

os.system(command): Run the specified command on the operating system.

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

import os
# Run cmd commands on the platform
os.system('cmd')
# Open g using Excel:\abc.xls file
os.startfile('g:\abc.xls')
os.spawnl(os.P_NOWAIT,'E:\Tools\Editing tools\Notepad++.7.5.6.bin.x64\notepad++.exe',' ')
# Use python command to execute os_test.py program
os.execl("D:\Python\Python36\python.exe"," ",'os_test.py','i')

If you run the above program directly, you can see that after the program is run, the abe.xls file is opened using Excel, the Notepad++ tool is also opened, and the os_test.py file is run using the python command. But if you uncomment the bolded code in the program, you will see that the cmd command line program is only started after the program runs. This is because when the as.system() function is used to run the program, the process where the new program is located will replace the original Process.

Knowledge point supplement:

os is the abbreviation of "operating system". As the name suggests, the os module provides an interface for various Python programs to interact with the operating system. By using the os module, on the one hand, you can easily interact with the operating system, on the other hand, the page can greatly enhance the portability of the code. If there is an error in the relevant function of the module, an OSError exception or its subclass exception will be thrown.

note

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

If it is a path-related operation, it is recommended to use the submodule os.path of os;

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

To create a temporary file or path, it is recommended to use the tempfile module;

To perform more advanced file and path operations, you should use the shutil module.

So far, this article about the os module and usage of Python learning is introduced here. For more related Python os modules and usage content, please search for ZaLou.Cn's previous articles or continue to browse the related articles below. Hope you will support ZaLou more in the future. .Cn!

Recommended Posts

Python learning os module and usage
Python3 module
Usage of os package in python
python learning route
Python and Go
python list learning
Python Lesson 37-Module
Detailed explanation of python standard library OS module
Detailed explanation of the usage of Python decimal module
Python introspection and reflection
Python entry learning materials
Python3 entry learning four.md
[python] python2 and python3 under ubuntu
Python3 external module use
python_ crawler basic learning
Python advanced usage summary
python_ regular expression learning
Prettytable module of python
Python3 entry learning three.md
Python deconstruction and packaging
Python3 entry learning one.md
Python3 configuration and entry.md
Python3 entry learning two.md
Python automated operation and maintenance 2
Python introduction and environment installation
Python high-order function usage summary!
Python know crawler and anti crawler
centos7 install python3 and ipython
ubuntu18.04 compile and install python3.8
Python regular expression quick learning
Centos 6.10 reinstall python and yum
Python programming Pycharm fast learning
Python open read and write
Getting started python learning steps
CentOS7 install python3 and pip3
Python automated operation and maintenance 1
Python data structure and algorithm
Python magic function eval () learning
Python multi-process and multi-thread basics
CentOS 6.9 compile and install python
matplotlib of python drawing module
Generators and iterators in Python
Python Faker data forgery module
Python high-order function usage summary!