Python can add a custom module
Method One:
echo ' Export pythonpath= '/root/pythondiy/' >>/ROOT/.BASHRC # This directory is the path to your module # then use the SYS module to view the environment variables import Syssys.path # return a list of [' ', '/usr/local/bin ',/root/pythondiy ', '/usr/local/lib/python27.zip ', '/usr/local/lib/python2.7 ', '/usr/ Local/lib/python2.7/plat-linux2 ', '/usr/local/lib/python2.7/lib-tk ', '/usr/local/lib/python2.7/lib-old ', '/usr/ Local/lib/python2.7/lib-dynload ', '/usr/local/lib/python2.7/site-packages ', '/usr/local/lib/python2.7/ Site-packages/setuptools-28.8.0-py2.7.egg ', '/usr/local/lib/python2.7/site-packages/pip-9.0.1-py2.7.egg ', '/usr/ Local/lib/python2.7/site-packages/ipython/extensions ', '/root/.ipython ']
Method Two:
Sys.path.append ('/root/pythondiy ') # This method is temporarily valid
2. Open opening file differences
Open ('/etc/passwd '). Read () # Returns the full text of the Stropen ('/etc/passwd '). ReadLines () # Returns the full text is a listopen ('/etc/passwd '). ReadLine () # reads a row each time and returns STR
3. Custom module Calls
Vim Wc.py#!/usr/bin/python from sys import argv def WC (s): chars = Len (s) words = Len (S.split ()) lines = S.cou NT (' \ n ') return lines,words,chars if __name__== ' __main__ ': # The function is called only when the script is executed. With Open (Argv[1]) as file1: print WC (File1.read ()) vim Copy_wc.pyimport
4. Implementation results
[Root@peng pyth]# python wc.py /etc/passwd # returns passwd file statistics (+, 1066) [Root@peng pyth]# python copy_wc.py< c14/># returns the Hosts file statistics (2, 10, 158)
5. Import the module package
# need to create a __init__.py empty file in the package, or it can be a description of the package Touch __init__.pyipythonfrom pyth Import WC # This will import a module package