Python file Operation exercises

Source: Internet
Author: User

Refer to the article written by a brother for practice: http://blog.51cto.com/000011211684/2052701

Main purpose, familiar with the file list of the increase, deletion, change operation ...


# python 3.6  File Open Operation practice #  date:2017-12-26# _*_ coding:utf-8 _*_import  os,sysdef find ():     find_name = input ("Please enter the name of the employee to be queried:"). Strip (). Upper ( )     flag=false    with open ("Info.txt", ' R ', encoding= ' utf-8 ')  as f1:        for line in f1:             if line.split () [0]==find_name:                 flag=True                 print ("Employee Name:", Find_name, "\ T salary: ", Line.split () [1])             else:                 continue     if not flag:         print ("Employee information not found: \033[31;1m{_name}\033[1m". Format (_name=find_ Name)) def modify ():     flag = false    mod_info1=input ( "Please enter the name of the employee to be modified:"). Strip (). Upper ()     with open ("Info.txt",  ' R ',  encoding= ' Utf-8 ')  as f1 ,         open ("Info_new",  ' W ',  encoding= ' Utf-8 ')  as f2:        for line in  f1:            if mod_info1 == Line.split () [0]:                 mod_info2 = input ("Please enter the revised Salary:"). Strip ()                  if not mod_info2.isdigit ():   #判断输入是否为数字       &nbsP;              print ("Please enter a number!! ")                      sys.exit ()    #需等待完善优化, same as                  else:                     f2.write (mod_info1+ ' \ t ' +mod_info2+ ' \ n ')                       flag = true            else:                  f2.write (line)      if os.path.exists (' Info_bak.txt '): Os.remove ("Info_bak.txt")   #如果存在则删除      os.rename ("Info.txt", "INfo_bak.txt ")     os.rename (" Info_new "," Info.txt ")     if flag:          print ("modified successfully")     else:          print ("Employee information not found: \033[31;1m{_name}\033[1m". Format (_NAME=MOD_INFO1)) def  add ():     flag = false    add_info1 = input ( "Please enter the name of the employee you want to add:"). Strip (). Upper ()     with open ("Info.txt",  ' R ',  encoding= ' Utf-8 ')  as f1 :        for line in f1:             if add_info1 ==  Line.split () [0]:                 flag = True            else:                 continue    if flag:         print ("Employee \033[31;1m{_name}\033[1m information already exists". Format (_NAME=ADD_INFO1))     else:        with open ("Info.txt",  ' A ',  encoding= ' utf-8 ')  as f2 :             add_info2 = input ("Please enter Salary:"). Strip ()              if not add_info2.isdigit ():  #  to determine if the input is digital                  print ("Please enter a number!! ")                 sys.exit ()             else:             &nbSp;   f2.write (add_info1 +  ' \ t '  + add_info2 +  ' \ n ')                  print ("added success!! ") Def dele ():    flag = false    del_info1 =  Input ("Please enter the employee name to delete:"). Strip (). Upper ()     with open ("Info.txt", ' R ', encoding= ' utf-8 ')  as f1 ,        open (' Info_new ', ' W ', encoding= ' utf-8 ')  as f2:        for line in f1:             if line.split () [0] == DEL_INFO1:                 flag=true                 continue             else:                 f2.write (line)     if os.path.exists (' Info_bak.txt '): Os.remove ("info _bak.txt ")   #如果存在则删除     os.rename (" Info.txt "," Info_bak.txt ")      Os.rename ("Info_new", "Info.txt")     if flag:         print ("Delete succeeded!! ")     else:        print (" Employee information not found: \033[31;1m{_name }\033[1m ". Format (_NAME=DEL_INFO1)) Def main ():    menu={          ' 1 ':find,         ' 2 ':modify,          ' 3 ':add,         ' 4 ':d ele,          ' 5 ': EXIT    }    WHILE TRUE:&NBSp;       print ("" "         Please select:         1, find employee information          2, modify employee information         3, increase employee information          4, delete employee information         5, exit system           ""  )         choice=input (' >>>> '). Strip ()          if choice in menu:             menu[choice] () if __name__== "__main__":     Main ()


Python file Operation exercises

Related Article

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.