Subsections
Module
Input and Output
Error Check
Class
Standard Library
VII. Modules
(1) module Import
# System module
Import OS, sys, shutil, logging
# Custom Module
Import proc, proc1
(2) module call
Proc. all_proc (Param, Param, Param)
Module search path
When importing a module called proc, the interpreter first searches the current directory for the module named Proc. in the directory list indicated by the Environment Variable pythonpath, and then in the path list of the Environment Variable path. If pythonpath is not set or the file is not found, search for the installation directory. D: \ python27.
(3) standard modules)
For details about the core module, refer to the python standard library.
1. built-in functions and exceptions
_ Builtin _ module and built-in exception exceptions
Define built-in functions (such as Len, Int, range...) and all exceptions.
Python imports these two modules at startup so that any program can use them.
2. Operating System Interface Module
They provide platform-independent interfaces for the underlying operating system.
Such modules include OS modules that provide file and process processing functions;
Provides the OS. Path module of the platform for independent file name processing (splitting directory name, file name, suffix, etc;
And the time/datetime module related to time and date processing.
3. Type Support Module
There are many libraries in the standard library that support built-in types of operations.
The string module implements common string processing.
The math module provides mathematical computation operations and constants (PI and E belong to these constants ),
The cmath module provides the same functions as math for the plural.
4. Regular Expression
The re module provides regular expressions for python. Regular expressions are used to match strings or special character strings in a string mode with specific syntax.
5. Language Support Module
The SYS module allows you to access the interpreter parameters, such as the module search path and interpreter version number.
The operator module provides the same function as the built-in operator. The copy module allows you to copy objects,
The newly added GC module of Python 2.0 provides related control functions for garbage collection.
(4) packages Package
A. B Indicates the sub-module named "A" in the package named "B. Using modules to save different module architectures can avoid conflicts between global variables, and using packages can avoid conflicts between modules.
8. Input and Output
(1). String output
Print
import osfileName = r"ac.txt";filePath=r"e:\ac.txt";print fileName,os.path.abspath(filePath)
Output:
Ac.txt E: \ ac.txt
OS. Path operations
OS. Path. abspath (PATH)
Returns the absolute path of path normalization.
OS .path.abspath('test.csv ')
'C: \ python25 \ test.csv'
OS. Path. Split (PATH)
Split the path into two groups: Directory and file name.
OS. Path. Split ('C: \ CSV \ test.csv ')
('C: \ CSV ', 'test.csv ')
OS. Path. dirname (PATH)
Returns the path directory. It is actually the first element of OS. Path. Split (PATH.
OS. Path. basename (PATH)
Returns the final file name of the path. If the path ends with a slash (/) or slash (\), a null value is returned. That is, the second element of OS. Path. Split (PATH.
OS. Path. exists (PATH)
If the path exists, true is returned. If the path does not exist, false is returned.
OS. Path. isfile (PATH)
Returns true if path is an existing file. Otherwise, false is returned.
OS. Path. isdir (PATH)
If path is an existing Directory, true is returned. Otherwise, false is returned.
OS. Path. Join (path1 [, path2 [,...])
After multiple paths are combined, the parameters before the first absolute path are ignored.
OS. Path. Join ('C: \ ', 'csv', 'test.csv ')
'C: \ CSV \ test.csv'
(2) reading and writing files
Open () returns a file. Generally, two parameters open (filename, mode) are required ).
The optional mode is 'R'. This option makes the file read-only;
'W'. This option only writes the file (for files with the same name, this operation overwrites the original file );
'A'. This option opens the file in append mode;
'R + '. This option opens the file in read/write mode;
If not specified, the default mode is 'R.
(3) file object operations
F = open (filepath, 'R ')
F. Read (size); this method reads a certain amount of data and returns its content as a string. The length of the string is the size specified by the value size. If no size is specified or a negative number is specified, the entire file is read and returned.
If the end of the file is reached, F. Read () returns an empty string ("").
F. Readline () reads a single line from the file. A line break is automatically added at the end of the string. This operation is ignored only when the last line of the file does not end with a line break. In this way, there will be no ambiguity in the returned value. If F. readline () returns an empty string, which indicates that it has reached the end of the file. If it is a blank line, it will be described as '\ n' and a string containing only line breaks.
F. Write (string) writes the string content to the file and returns none.
Value = (the answer, 42)
S = STR (value) # '('the ancer', 42 )'
F. Write (s)
IX. Error Check
10. Category
Format
Class myobject (base ):
Body
The base parameter is a parent class, and the object class is a subclass of all classes.
Eg:
#! /Usr/bin/Python #-*-coding: UTF-8-*-''' created on 2012-12-17 @ Author: chenll ''' class persion (object ): 'class document' # class Document # define the constructor def _ init _ (self, nm, pH): # note _ init __, which contains two underscores, instead of _ init _ self. name = Nm # Set Name self. phone = Ph # Set phone def printper (Self): Print 'my persion' + self. name + ',' + self. phone # self indicates the instance object itself def main (): mypersion = persion ('chen', '123') mypersion. name mypersion. phone mypersion. printper () If _ name _ = "_ main _": Main ();
11. Standard Library
The core of Python only contains common types and functions such as numbers, strings, lists, dictionaries, and files, the Python Standard Library provides additional functions such as system management, network communication, text processing, database interfaces, graphic systems, and XML processing.
The main functions of the python standard library are:
Text processing, including text formatting, regular expression matching, text difference calculation and merging, Unicode support, binary data processing, and other functions
File processing, including file operations, creating temporary files, File compression and archiving, and operating configuration files
Operating system functions, including thread and process support, Io reuse, date and time processing, calling system functions, and logging
Network communication, including network sockets, SSL encrypted communication, asynchronous network communication, and other functions
Network protocol, supports multiple network protocols such as HTTP, FTP, SMTP, Pop, IMAP, nntp, and XMLRPC, and provides a framework for compiling network servers.
W3C format support, including HTML, SGML, and XML processing.
Other functions, including international support, mathematical operations, hash, tkinter, etc.
#! /Usr/bin/Python #-*-coding: UTF-8-*-''' created on 2012-12-17 @ Author: chenll ''' import sysdef main (): ''' (1), SYS module. The name of the currently running program, which is used as the SYS. argv [0] other command line parameters after this project. '''If Len (sys. argv)> 0: print sys. argv [0]; if Len (sys. argv)> 1: print sys. argv [1]; ''' D: \ workspaces \ Python \ com \ mapbar \ Python> Python helloworld1.py x helloworld1.py x ''' (2), OS module, this module contains common operating system functions. this module is especially important if you want your program to be unrelated to the platform. For example, OS. SEP can be used to replace the path delimiter specified by the operating system. The OS. Name string indicates the platform you are using. For example, for Windows, it is 'nt ', and for Linux/Unix users, it is 'posix '. The OS. getcwd () function obtains the current working directory, that is, the directory path of the current Python script. The OS. getenv () and OS. putenv () functions are used to read and set environment variables respectively. OS. listdir () returns all files and directory names in the specified directory. The OS. Remove () function is used to delete an object. The OS. System () function is used to run shell commands. The OS. linesep string specifies the row Terminator used by the current platform. For example, in windows, '\ r \ n' is used, in Linux,' \ n' is used, and in MAC, '\ R' is used '. OS. path. the split () function returns the directory name and file name of a path, OS. path. isfile () and OS. path. the isdir () function checks whether the given path is a file or a directory. The OS. Path. Existe () function is used to check whether the GIVEN PATH actually exists. '''If _ name _ = "_ main _": Main ();