Getting day2 python

File processing module:

# File read and write, using 
# Open 
# text operation 
# ( 'absolute path name of the file (copy path) / .txt file', the operation mode, the specified character coding) Open 
# F: handle call 
# R & lt: prevent escape character 
# open the file will have two resources: 1, and the python interpreter python resource files, the program will automatically recovered python end 
# 2, operating systems open the resource file, the file is opened, the operating system does not to help us recover, we need to manually recover 
# f = Open ( 'name of the file .txt', mode = 'wt' , encoding = 'utf-8') # amount bags in a new file called the name of the file writing, and the original document does not exist inside the content 
# f.write ( '! Tank Hello') 
# f.close () 

# read file 

F = Open (R & lt ' \ C: \ the Users \ yeting \ PycharmProjects \ Untitled \ 02 \ name of the file .txt ' , ' r ' ,encoding=' UTF-. 8 ' )   # Default RT 
RES = reached, f.read ()
 Print (RES) 

f.close () 




# file addition 
F = Open (R & lt ' \ C: \ the Users \ yeting \ PycharmProjects \ Untitled \ 02 \ file name of .txt ' , ' a ' , encoding = ' UTF-8 ' )   # default aT 
f.write ( ' the Hello, limengfan ' )   # adding new content in the original document 
f.close () 


file processing only context management : with 
with built will close () function to automatically shut down 
with Open (R & lt ' li.txt ' ,
           'w',encoding='utf-8') as f:
    f.write('life is short,u need python')
with open(r'li.txt',
          'r',encoding='utf-8') as f:
         res = f.read()
         print(res)

with open(r'li.txt',
          'r', encoding='utf-8') as f:
    res = f.read()
    Print (RES)
 Import Requests 
RES = requests.get ( ' URL ' )
 Print (res.content) # Pictures of the word flow
View Code

 

 

•  pictures and video module reads and writes:

Import Requests 
RES = requests.get ( " http://news.mydrivers.com/Img/20120301/2012030102215971.jpg " )
 Print (res.content)
 # write the picture 
with Open ( ' scenery .jpg ' , ' wb ' ) AS f: 
    f.write (res.content) 

# read the image 
with Open ( ' scenery .jpg ' , ' rb ' ) AS f: 
    RES = f.read ()
     Print (RES) 

# file copy 
with Open ( ' landscape .jpg ' ,' RB ' ) AS F, Open ( ' Landscape .jpg ' , ' WB ' ) AS W: 
    RES = reached, f.read () 
    w.write (RES) 


# write video 
with Open ( ' video files .mp4 ' , ' RB ' ) AS F, open ( ' video files -copy.mp4 ' , ' WB ' ) AS W: 
    RES = reached, f.read () # once open all the contents, if the file size exceeds the size of the memory can cause memory overflow 
    Print (RES ) 
     w.write (RES) 

# line by line reading documents 
with Open ( 'tianyan_sys.mp4 ' , ' RB ' ) AS F, Open ( ' tianyancopy.mp4 ' , ' WB ' ) AS W:
         for Line in F: # line by line read the contents of the file, write the file line by line, to avoid memory overflow 
          w.write (line)
View Code

  Function modules:

# 1, what is the function 
# 2, (how to use the function) 
#   define, after a call to 
# 3, grammatical function 
# DEF: definition of 

# DEF function name (parameter 1, parameter 2, .....): 
#      " "" 
      # comment 
#      function declarations 
#      "" " 
#      function body codes (logic code) 
#        return value return 
# 
# DEF :( full defind) to declare keyword user-defined functions 
# function name: see his name know Italy 
# (): brackets, storage is to accept external parameter 
# Note: to illustrate the function returns 
# function body codes: logic code 
# return: the return value of 




# registration function 

# thing happened definition phase function 
# 1, first open the python interpreter
# 2, load the python file 
# . 3, detect only python explained syntax, but will only detect python syntax, the code will not execute the function body 


# define 
DEF Register ():
     the while True: 
        User = INPUT ( ' Enter Username : ' ) .strip () 
        pwd = iNPUT ( ' enter password: ' ) .strip () 
        re_pwd = iNPUT ( ' enter password: ' ) .strip () 


        # determines whether the password twice consistent 
        iF pwd == re_pwd :
             # formatting characters fly 

            # uer_info = 'username:% s, password: S%'% (user, PED) 
            # uer_info = 'user name:} {password: {}'. format (user , pwd)
            # Write f corresponds to a string before calling format method 
            USER_INFO = f ' Username: {user}, password: {} pwd ' 

            with Open (f ' {} User .txt ' , ' W ' , encoding = ' UTF -8 ' ) AS f: 
                f.write (user_info) 

            BREAK 
        the else :
             Print ( ' ! Please re-enter the password twice inconsistent ' ) 

# call the function function name () function call that is 
# egister () 

DEF foo ():
     Print ( ' from foo! ' ) 
    bar () 
    #Print ( 
# call phase, the code will execute the function body foo 
foo ()
View Code

Homework:

DEF Login (): 
    I =. 1
     the while I <=. 3 : 
        User = INPUT ( ' Enter Username: ' ) .strip () 
        pwd = INPUT ( ' Enter password: ' ) .strip () 
        with Open ( ' yeting .txt ' , ' R & lt ' , encoding = ' UTF-. 8 ' ) AS F: 
            old_info = reached, f.read (.) Split ( ' , ' ) 

        for index in Range (len (old_info)):
             IFold_info [index] == the User:
                 IF old_info [1 + index] == pwd:
                     Print ( " Login successful " ) 
                    i = 4
                 BREAK 
            the else :
                 Print ( " user name or password error " ) 
                i + = 1
                 BREAK 

the Login ()
View Code
 
 

Today Summary:

      The main study of the remaining data types built-in method, a basic understanding of the character encoding and file handling, and basic functions. Solve a lot of practice in the teacher's help

Problems encountered in the process, gain a lot. There are many things to learn, continue to fuel!

 

 

Guess you like

Origin www.cnblogs.com/yt2223/p/11012079.html