My Day1, myday1
I. user input
When entering the password, if you want to be invisible, you need to use the getpass method in the getpass module, that is:
#! /Usr/bin/env python #-*-coding: UTF-8-*-import getpass # assign the user input content to the name variable pwd = getpass. getpass ("Enter Password:") # print the entered content print (pwd)Ii. Modules
Python is powerful because it has a rich and powerful standard library and third-party library. Almost any function you want to implement has the corresponding Python library support, in future courses, we will explain in depth the various frequently-used libraries. Now, let's start with a symbolic study of two simple ones.
Sys
#! /Usr/bin/env python #-*-coding: UTF-8-*-import sys print (sys. argv) # output $ python test. py helo world ['test. py', 'HELO', 'World'] # obtain the parameters passed during script execution.
OS
#! /Usr/bin/env python #-*-coding: UTF-8-*-import OS. system ("df-h") # Call system commands
Fully integrated
Import OS, sys OS. system (''. join (sys. argv [1:]) # Use your input parameters as a command and submit it to OS. system for execution.
Write a module by yourself
Python tab completion Module
1 import sys2 import readline3 import rlcompleter4 5 if sys.platform == 'darwin' and sys.version_info[0] == 2:6 readline.parse_and_bind("bind ^I rl_complete")7 else:8 readline.parse_and_bind("tab: complete") # linux and python3 on macFor MAC
1 #!/usr/bin/env python 2 # python startup file 3 import sys 4 import readline 5 import rlcompleter 6 import atexit 7 import os 8 # tab completion 9 readline.parse_and_bind('tab: complete')10 # history file 11 histfile = os.path.join(os.environ['HOME'], '.pythonhistory')12 try:13 readline.read_history_file(histfile)14 except IOError:15 pass16 atexit.register(readline.write_history_file, histfile)17 del os, histfile, readline, rlcompleterFor Linux
You can use it after saving it.
localhost:~ jieli$ pythonPython 2.7.10 (default, Oct 23 2015, 18:05:06)[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwinType "help", "copyright", "credits" or "license" for more information.>>> import tab
- You will find that the tab. py module you wrote above can only be imported in the current directory. What if you want to use it in any part of the system? Now you need to put this tab. py is stored in the python global environment variable Directory, which is generally stored in a directory named Python/2.7/site-packages. The directory is placed in different operating systems in different locations, print (sys. path) to view the python Environment Variable list.