. log#!/usr/bin/python27#-*-coding:utf-8-*-Filelist1=os.listdir ('/var/log') Example1:s1='Hello.log'>>>s1.endswith ('. Log')>>>Trueexample2: Find all the files in Filelist1 that end with log filelist2=[i forIinchFilelist1ifI.endswith ('. Log')] PrintFilelist2 List Resolution: Generate a new list based on an existing list: L1=['x','y','Z']l2=[1,2,3]l3=[(I,J) forIinchL1 forJinchL2]Printl3[('x', 1), ('x', 2).....] OR L1=['x','y','Z']l2=[1,2,3]l3=[(I,J) forIinchL1 forJinchL2ifJ!=1]PrintL3 Generator expression: The generator expression does not actually create a list of numbers, but instead returns a generator object that, after each calculation of a directory, ' generates ' the entry (yield') out grammar: (Expr forIter_varinchiterabl) (Expr forIter_varinchIterablifcond_expr) Example: [I**2 forIinchRange (1,11)]g1= (i**2 forIinchRange (1,11) ) G1.next ()1G1.next ()4... Generates offsets and elements: the Enumeraterange can generate an index offset in a non-exhaustive traversal. If you need both an index and an offset element, you can use the enumerate built-in function to return a generator object s=' HELLO WORD ' E=Enumerate (S) E.next () (0,'H') E.next () (0,'E') file system OS and files:1, files are computer-managed by OS with name and storage area2, on Linux systems, files are treated as byte sequence files-----01010101010 (File stream)-----Process python Open file: Python built-in function open () opens a file and creates a file (open (Name[,model[,bufsize]]) The open method can accept three parameters: file name, mode, and the buffer parameter of the Open function returns a File object mode: Specifies the file open pattern BufSize: Defines the output cache: 0 means no cache 1 is represented by a cache negative number indicates that using the system default positive number indicates the use of approximate specified size buffer file open mode: R: Read Only open ('/var/log/message.log','R') W: Write A; Append is only used after the mode '+ ' means to agree to input/output r+,w+,a+Append ' B ' after the pattern'indicates that the file is opened in binary binaryrb,wb+. Log#!/usr/bin/python27#-*-coding:utf-8-*-ImportOSImportOs.pathfilename='/tep/test.txt'ifos.path.isfile (filename): F=open (filename,'A +') whileTrue:line=raw_input ('Enter something>') ifline=='Q' orline=='quit': Break; F.write ( Line+'\ n'f1.close (); function: is the basic program structure provided for the maximum reuse of code and the redundancy of minimizing code Pyhton:4 functions Global Local method Lambad: expression creation function: syntax:deffunctionname (Parameters): Suitereturntrue in the local scope of the function definition, the module defines the global scope lambed function F20=Lambdax,y:x+y>>>f20 (3,4) >>> 7defF20 (x, y):returnx+y-Functional programming can be called functional programming, which is a programming paradigm. He treats computer operations as mathematical function calculations and avoids states and data that can be compiled. Filter Filters: Filter ()defstartpos (m,n):defnewpos (x, y):Print "the old position are (%d,%d), and the new position is (%d,%d)"% (m,n,m+x,n+y)returnNewpos>>>action=startpos (10,10) >>>action () >>> the old position is(10,10), andThe new position is(11,12) >>> Action ( -1,3) >>> the old position is(10,10), andThe new position is(9,13) Example: forIinch(j**2 forJinchRange (1,11)): PrintI example:defDeco (func):defwrapper ():Print "Place say somthing"func ()Print "no Zou no die ... .." returnWrapper@decodefShow ():return "I am from Mars."Show () Place say Somthingno Zou no die .... Object-oriented (OOP): {encapsulation, inheritance, polymorphism} process-oriented: Programs= instruction +The data code can optionally be written as a command-centric or data-centric, with directives as the core: "Write" class relationships around what is happening
Python Learning 2