Python custom Modules

Source: Internet
Author: User

    1. 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

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.