Python Module initial knowledge

Source: Internet
Author: User

    

Summer vacation, tomorrow will go home, write an article this evening, after a few days did not have the opportunity to write ....

    

    Module Initial Knowledge:

      Module: is to encapsulate a bunch of functions together, directly into the use

Modules, also known as libraries, fall into two main categories:

Standard library: No need to install libraries that can be referenced directly

Third party libraries: libraries that must be installed to use

See what libraries are there. In the Python installation directory, in the Lib directory:

        

Here is the standard library.

In the Lib directory site-packages directory is a third-party library, I can not ...

    Let's look at the next two modules: SYS and OS

        Sys

Let's take a look at the path function under SYS

  

1 # Author: Learning 2 3 Import Sys; 4 5 Print (Sys.path);

        The function that we invoke the module with is "."

In order to look at the convenience of me to change the line ...

 

Path function, which holds Python's environment variables (note: Python, not system environment variable), the Py reference module or some path of internal call

We can directly refer to the module where the file is definitely stored in the path above ...

Another function of looking at SYS: argv

ARGV is a list, simply take a look.

1 # Author: Learning 2 3 Import Sys; 4 5 # print (sys.path); 6 7 Print (SYS.ARGV);

      

You can see that I executed two times, respectively, there are parameters and no parameters, the result of execution is not the same, this is a list, then how do we take out the value of it?

Like I'm going to take out the path here. So in the code only need to modify sys.argv[0], here argv followed by a bracket, note: The value inside is not the value to be printed. It's like an array, 0 is his subscript, the first one is 0, the second is 1, And so on. The calculation starts at 0. We write a 0 on this side, the result of printing is naturally the current path.

  

  

OK, we have this module for the time being said these two functions, others can self-check manual.

Next look at today's another module: 0S

OS: Some related to the system.

For example: We use the OS to execute system commands:

      

1 # Author: Learning 2 3 Import os; 4 5 5 Os.system ("dir");

The system function can be used to execute the command, could we save the value to a variable?

Try:

    

1 Import os; 2 3 cmd_result = Os.system ("dir"); 4 5 Print (Cmd_result);

  

    You can see that there is a 0, know: The system directly prints the execution results to the screen, and save to the variable is only successful execution, success: 0, Failure: 1

You can write a wrong command yourself and try to print out the results.

What do I have to save to a variable? I can't sleep without saving. Don't worry, use the Popen function.

    Execution results What the hell is this? This is the address of the object that is stored in memory. What's the use of him? Get the value from this address. How do I get it? Read () function

    You can see that by adding a read () to the back of the Popen, you can get his value, and simply understand that the result of execution is stored somewhere in the memory and read it through Read ().

The OS is here, and this module will certainly be in detail. Here's a small example: command execution.

    

Code:

  

1 #Author: Learning2 3 Importos;4 5 Print("Exit Please enter N or n");6  whileTrue:7cmd = input ("Please enter a command:");8     ifcmd = ='N' orcmd = ='N':9         Print("exit command Execution!!!");Ten          Break; One Os.system (cmd); A     

    OS mkdir Features: Os.mkdir ("folder name"); I'm not going to say that, it's the new folder.

    

    We were talking about third-party libraries at the very beginning of the article, so can we write one?

I'm here to write a user login interface to see

    

      Is this okay? We're creating a new file to introduce login.py.

    

This is OK, do not feel special nonsense, think I am lying to you? Unfortunately, this is the module, and the file contained in PHP is the same

Note: My path here is relative path, you can go back to see the value of Sys.path, the first is the path to execute the file, there are other environment variable path, if you login.py into the non-environment variable path, then you will be prompted error module can not find. OK, the simple knowledge of the module is written to this, when it is okay to take a look at the PY module, see the Big god of the coquettish code ....

Python Module initial knowledge

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.