Python day2, pythonday2

Source: Internet
Author: User

Python day2, pythonday2

 

I. Initial Knowledge of the module

Convenient directory:

Sys. path: gets the string set of the specified module's search path (currently sys)

Sys. argv transmits parameters from an external program to an internal program

Sys. getdefaultencoding () Get the current system code

sys.getfilesystemencoding()The file system is encoded. 'mbcs 'is returned in Windows, and 'utf-8' is returned in mac '.

Sys module:

1. Environment (installed on D: \ Anacinda3, python3.5)

2. Common functions of the sys module

(1) sys. path: Get the string set of the specified module's search path (currently sys)

Sys. path

Run the above code to get the environment of my sys module, print the absolute path under pycharm, and print the relative path under cmd.

Result

['D: \ Python_train \ day2 ',
'D: \ Python_train ',
'D: \ Anaconda3 \ python35.zip ',
'D: \ Anaconda3 \ DLLs ',
'D: \ Anaconda3 \ lib ', # store the standard library
'D: \ Anaconda3 ',
'D: \ Anaconda3 \ lib \ site-packages ', # The required third-party libraries can be imported directly here.
'D: \ Anaconda3 \ lib \ site-packages \ Sphinx-1.4.6-py3.5.egg ',
'D: \ Anaconda3 \ lib \ site-packages \ win32 ',
'D: \ Anaconda3 \ lib \ site-packages \ win32 \ lib ',
'D: \ Anaconda3 \ lib \ site-packages \ Pythonwin ',
'D: \ Anaconda3 \ lib \ site-packages \ setuptools-27.2.0-py3.5.egg ']

(2) sys. argv transmits parameters from an external program to an internal program

 1 #!/usr/bin/env python 2 #-*- Coding:utf-8 -*- 3 # Author:Eric.Shen 4  5 import  sys 6  7 print(sys.argv) 8 print(sys.argv[0]) 9 print(sys.argv[1])10 print(sys.argv[2])11 print(sys.argv[3])
Sys. argv

Passing a parameter from an external program to argv returns test at 0 for the first time. py: returns 1 at position 1 for the second time. Similarly, you can pass parameters to the program during the test to know what operations to perform.

Run this file in cmd and pass three parameters to test at the same time. The running result is as follows:

(3) sys. getdefaultencoding () Get the current system code

1 #! /Usr/bin/env python2 #-*-Coding: UTF-8-*-3 # Author: Eric. shen4 5 import sys6 7 encoding = sys. getdefaultencoding () # Get the current system code 8 print (encoding)
Sys. getdefaultencoding ()

As mentioned above: Get the current encoding of the system. The running result is as follows:

(4)sys.getfilesystemencoding(): The file system is encoded. 'mbcs 'is returned in Windows, and 'utf-8' is returned in mac '.

1 #! /Usr/bin/env python2 #-*-Coding: UTF-8-*-3 # Author: Eric. shen4 5 import sys6 encoding = sys. getfilesystemencoding () # Get the encoding method used by the file system 7 print (encoding)
Sys. getfilesystemencoding ()

 

The execution result is as follows:

(5) sys. platform: Obtain the current system platform

1 #! /Usr/bin/env python2 #-*-Coding: UTF-8-*-3 # Author: Eric. Shen4 5 import sys6 7 print (sys. platform) # obtain the current system platform
Sys. platform

Run the following command:

(6) sys. stdin \ stdout \ stderr

Stdin, stdout, and stderr variables contain the stream object corresponding to the standard I/O Stream. if you need to better control the output, and print cannot meet your requirements, they are what you need. you can also replace them. In this case, you can redirect the output and input to other devices, or process them in a non-standard way (arc: basic usage is not cleared, and will be supplemented later)

1 #!/usr/bin/env python2 #-*- Coding:utf-8 -*-3 # Author:Eric.Shen4 5 import  sys6 7 print(sys.stdin,"\n",sys.stdout,"\n",sys.stderr)
Sys. stdin \ stdout \ stderr

 

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.