The difference between OS and sys two modules in Python

The official explanations of the OS and SYS modules of the reproduced article are as follows:

Os: this module provides a portable the by using operating system dependent functionality.

This module provides a convenient way to use the operating system functions.

Sys: this module provides access to some variables used or maintained by the interpreter and to functions that interact St Rongly with the interpreter.

This module allows access to variables used or maintained by the interpreter and functions that interact with the interpreter.

The summary is that the OS module is responsible for interacting with the operating system, providing access to the underlying interface of the operating system, and the SYS module is responsible for the interaction between the program and the Python interpreter, providing a series of functions and variables for manipulating the Python runtime environment.

Os Common methods

Os.remove (‘ path/filename ‘) Delete file

Os.rename (Oldname, newname) renaming files

Os.walk () Generate all file names under the directory tree

Os.chdir (‘ dirname ‘) Change directory

Os.mkdir/makedirs (‘ dirname ‘) create a directory/multi-level directory

Os.rmdir/removedirs (‘ dirname ‘) Delete directory/multi-level directory

Os.listdir (‘ dirname ‘) lists the files for the specified directory

OS.GETCWD () Get the current working directory

Os.chmod () Changing directory permissions

Os.path.basename (‘ path/filename ‘) remove directory path, return file name

Os.path.dirname (‘ path/filename ‘) remove file name, return directory path

Os.path.join (path1[,path2[,…]]) combines the parts of the separation into one path name

Os.path.split (‘ path ‘) returns (DirName (), basename ()) tuple

Os.path.splitext () return (filename, extension) tuple

Os.path.getatime\ctime\mtime returns the last access, creation, modification time, respectively

Os.path.getsize () returns the file size

Os.path.exists () is present

Os.path.isabs () is an absolute path

Os.path.isdir () is a directory

Os.path.isfile () is a file

SYS Common Methods

SYS.ARGV command line argument list, the first element is the path of the program itself

Sys.modules.keys () returns the list of all modules that have been imported

Sys.exc_info () Gets the exception class currently being processed, Exc_type, Exc_value, exc_traceback the exception details currently handled

Sys.exit (n) exit program, Exit normally (0)

Sys.hexversion gets the version value of the Python interpreter, 16 binary format such as: 0x020403f0

Sys.version get version information for Python interpreter

Sys.maxint the largest int value

Sys.maxunicode the largest Unicode value

Sys.modules returns the module field of the system import, key is the module name, value is the module

Sys.path returns the search path for the module, using the value of the PYTHONPATH environment variable when initializing

Sys.platform returns the operating system platform name

Sys.stdout Standard Output

Sys.stdin Standard input

Sys.stderr Error Output

Sys.exc_clear () to clear current or recent error messages that are present on the current thread

Sys.exec_prefix returns the location of the platform standalone Python file installation

Sys.byteorder The local byte rule indicator, the value of the Big-endian platform is ‘ big ‘, the value of the Little-endian platform is ‘ little ‘

Sys.copyright record python copyright-related things

API version of C for the Sys.api_version interpreter


stdin, stdout, and stderr variables contain stream objects that correspond to standard I/O streams. If you need more control over the output, and print does not meet your requirements, they are what you need. You can also replace them, so you can redirect output and input to other devices, or handle them in a non-standard way.We often use print and raw_input to input and print, then How does print and raw_input relate to standard input/output streams?
In fact, the Python program’s standard input/output/error stream is defined in the SYS module, respectively: Sys.stdin,sys.stdout, Sys.stderr
The following programs can also be used to input and output the same:
import sys


print ‘Please enter yourname:‘,
print ‘Hi, %s!‘ % name

So Sys.stdin, Sys.stdout, stderr exactly what is it? We enter the following code in the Python Runtime environment:
import sys
for f in (sys.stdin,sys.stdout, sys.stderr): print f
<open file‘<stdin>‘, mode ‘r‘ at 892210>
<open file‘<stdout>‘, mode ‘w‘ at 892270>
<open file‘<stderr>‘, mode ‘w at 8922d0>

It can be seen that stdin, stdout, stderr in Python are nothing more than file attributes, they are automatically associated with the standard input, output, and error in the shell environment when Python starts.
The I/O redirection of the Python program in the shell is exactly the same as the DOS command at the beginning of this article, which is actually provided by the shell and is not related to python itself. So can we redirect Stdin,stdout,stderr Read and write operations to an internal object inside a python program? The answer is yes.
Python provides a Stringio module to complete this idea, such as:
From Stringio import Stringio
Import Sys
Buff =stringio ()

Temp =sys.stdout #Save standard I/o Flow
Sys.stdout =buff #Redirect the standard I/o flow to Buff object
Print, ‘hello’, 0.001

Sys.stdout=temp #Restore standard I/O Flow

Original Source:




Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:

Love podcasts or audiobooks? Learn on the go with our new app.

Alibaba Cloud Big Data Solutions for Real-Time Analysis and Decision-Making for Enterprises

Count Ways To Reach The N-th Stairs

Learn Software Like It’s 2009

comparison between the technology stack of 2009 (only jquery) and the technology stack in 2019 (9 different technologies)

Future Development Trends of Cloud-Native

Domain-Driven Design — First impressions

Go-clean-template: Clean Architecture template for Golang services

Making Open-Source Accessible for All

Software Development Trends of 2022

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:

More from Medium

A “reference” for Python Variables

Installing Multiple Python Versions on Windows 10

Deploy Python Application with pyzbar package to Heroku

Installing Python for Beginners