Python3 to the local TXT file to add, delete, modify, view function.

Source: Internet
Author: User

Because is the beginner, the code if has insufficient, welcome to point out!

This blog record my way of programming, record the knowledge learned, share the learning experience!

This is one of my homework.

The first analysis requires:

    1. Create a TXT file to store your account number and password
    2. Implement the ability to add, delete, modify, and view files.

Analysis Ideas:

    1. Write a menu of choices first
    2. List the custom functions we want to write
    3. The main way to implement add, delete, modify, and view functions is to read the file to return a list and then add, delete, modify, and view the list and then write the list to the file.

1 defJia (LIST):#1 Adding functions2     Print('blank plus enter to exit! ')3      whileTrue:4S = input ('Enter your account:')5         if(s==' '):6              Break7S2=input ('Enter Password:')8List.append (s+'    '+s2+'\ n')9With open ('Wj-4.txt','W') as F:Ten f.writelines (LIST) One  A defCha (LIST):#2 Viewing Functions -     Print('the contents of the file are as follows:') -      forIinchLIST: the         Print(I.strip ('\ n')) -Input"Press any key to return to the menu! ") - Cai (LIST) -  + defShan (LIST):#3 Deleting a function -     Print('input-1 exit!') +      whileTrue: AR = Int (input ('Please enter to delete the first few:')) at         if(R==-1): -              Break -         delList[r-1] -With open ('Wj-4.txt','W') as F: - f.writelines (LIST) -  in defGai (LIST):#5 Modifying functions -     Print('input-1 exit! ') to      whileTrue: +R = Int (input ('Please enter the modification of the first few:')) -         if(R==-1): the              Break *R2 = input ('Enter the modified character:') $         delList[r-1]Panax NotoginsengList.insert (R-1, R2) -         Print(LIST) the  + defDu ():#Read file Functions AWith open ('Wj-4.txt','R') as F: theLIST =F.readlines () +     returnLIST -  $ defCai (LIST):#Menu Functions $      whileTrue: -         Print('menu selection: \ n (1) Add function \ n (2) view function \ n (3) Delete function \ n (4) modify function \ n (5) Exit') -A=int (Input ('Please enter:')) the         if(a==1): - Jia (LIST)Wuyi         elif(a==2): the cha (LIST) -         elif(a==3): Wu Shan (LIST) -         elif(a==4): About gai (LIST) $         elif(a==5): -             Print('Exit Success! \ n Welcome to use next time! ') -              Break -         Else:Print('Please enter your selection correctly!') A  + if __name__=='__main__': the     Try: -LIST = Du ()#Return to list $Cai (LIST)#Menu the     except:Print('An unknown error has occurred! ')

Test diagram:

This content mainly examines the familiar with the list and the file.

Python3 to the local TXT file to add, delete, modify, view function.

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.