-
Python can add a custom module
Method one: Echo ' export pythonpath= '/root/pythondiy/' >>/ROOT/.BASHRC # This directory is the path to your module # and 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/lo Cal/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 an L Istopen ('/etc/passwd '). ReadLine () # reads one line at a time and returns STR
3. Custom module Calls
Vim wc.py#!/usr/bin/pythonfrom sys import argvdef WC (s): chars = Len (s) words = Len (s.split ()) lines = S.count (' \ n ') return lines,words,charsif __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
[[email protected] pyth]# Python wc.py/etc/passwd # Return to passwd file statistics (+, 1066) [[email protected] pyth]# python copy _wc.py # Back to 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 # So import a module package
This article is from the "Stranded," blog, please be sure to keep this source http://yasar.blog.51cto.com/9120455/1874832
Python custom Modules