The difference between OS and sys two modules in Python

Source: Internet
Author: User
Tags stdin

os and sys 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









Sys.stdin,sys.stdout,sys.stderr


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

sys.stdout.write(‘HelloWorld!‘)

print ‘Please enter yourname:‘,
name=sys.stdin.readline()[:-1]
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
output:
<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

Print Buff.getvalue ()


The difference between OS and sys two modules in Python


Alibaba Cloud Hot Products

Elastic Compute Service (ECS) Dedicated Host (DDH) ApsaraDB RDS for MySQL (RDS) ApsaraDB for PolarDB(PolarDB) AnalyticDB for PostgreSQL (ADB for PG)
AnalyticDB for MySQL(ADB for MySQL) Data Transmission Service (DTS) Server Load Balancer (SLB) Global Accelerator (GA) Cloud Enterprise Network (CEN)
Object Storage Service (OSS) Content Delivery Network (CDN) Short Message Service (SMS) Container Service for Kubernetes (ACK) Data Lake Analytics (DLA)

ApsaraDB for Redis (Redis)

ApsaraDB for MongoDB (MongoDB) NAT Gateway VPN Gateway Cloud Firewall
Anti-DDoS Web Application Firewall (WAF) Log Service DataWorks MaxCompute
Elastic MapReduce (EMR) Elasticsearch

Alibaba Cloud Free Trail

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.