The difference between OS and sys two modules in Python
official explanations of <os and sys >
? OS
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
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.
? Summary
The OS module is responsible for the interaction between the program and the operating system, provides 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 the requirements , they are required. You can also
Replace them, and you can redirect output and input to other devices, or handle them in a non-standard way
import syssys.stdout.write(‘HelloWorld!‘)print ‘Please enter yourname:‘,name=sys.stdin.readline()[:-1]print ‘Hi, %s!‘ % name
Print and raw_input are commonly used for input and printing, so
how print and raw_input relate to standard input/output streams: In fact, the standard input/output/error stream of a Python program 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, enter the following code in the Python runtime environment:
import sysfor f in (sys.stdin,sys.stdout, sys.stderr): print f
the output is:
<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. Then we can redirect the
Stdin,stdout,stderr read-write operation inside the Python program to an internal object.
python provides a stringio module to complete this idea, such as:
from StringIO import StringIOimport sysbuff =StringIO()temp =sys.stdout #保存标准I/O流sys.stdout =buff #将标准I/O流重定向到buff对象print 42, ‘hello‘, 0.001sys.stdout=temp #恢复标准I/O流print buff.getvalue()
<wiz_tmp_tag id= "Wiz-table-range-border" contenteditable= "false" style= "Display:none;" >
Python Learning _ Interpreter interface module: SYS