Python Small note 1

Source: Internet
Author: User

How to read and write files:

R opens the file in read-only mode. The pointer to the file will be placed at the beginning of the file. This is the default mode.
r+ open a file for read-write. The file pointer will be placed at the beginning of the file.
W opens a file for writing only. Overwrite the file if it already exists. If the file does not exist, create a new file.
w+ open a file for read-write. Overwrite the file if it already exists. If the file does not exist, create a new file.
A opens a file for appending. If the file already exists, the file pointer will be placed at the end of the file. In other words, the new content will be written to the existing content. If the file does not exist, create a new file to write to.
A + opens a file for read and write. If the file already exists, the file pointer will be placed at the end of the file. The file opens with an append mode. If the file does not exist, create a new file to read and write.


RB opens a file in binary format for read-only. The file pointer will be placed at the beginning of the file. This is the default mode. Generally used for non-text files and so on.
rb+ opens a file in binary format for read-write. The file pointer will be placed at the beginning of the file. Generally used for non-text files and so on.
WB opens a file in binary format for writing only. Overwrite the file if it already exists. If the file does not exist, create a new file. Generally used for non-text files and so on.
wb+ opens a file in binary format for read-write. Overwrite the file if it already exists. If the file does not exist, create a new file. Generally used for non-text files and so on.
AB opens a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file. In other words, the new content will be written to the existing content. If the file does not exist, create a new file to write to.
ab+ opens a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file. If the file does not exist, create a new file to read and write.

Fpath = R ' D:\tmp\account.txt '

Def user_register ():
' User registration, registration successful return true, failed return false '
Username = raw_input ("Enter user name:")
f = open (Fpath)
For line in F:
account = Line.strip (). Split (' # ')
If username = = Account[0]:
Return False

F.close ()
Password = raw_input (' Please enter password: ')
f = open (Fpath, ' a ')
F.write ('%s#%s\n '% (Username,password))
F.close ()
Return True

Def user_login ():
' User login, successfully returned 0, the user does not exist return-1, password error returned-2 '
Username = raw_input (' Please enter user name: ')
f = open (Fpath)
For line in F:
account = Line.strip (). Split (' # ')
If username = = Account[0]:
Password = raw_input (' Please enter password: ')
if password = = Account[1]:
return 0
Else
Return-2
Break
Else
Return-1
F.cloes ()


def main ():
While True:
Print "1, user Registration"
Print "2. Login"
Print "3. Exit"
opt = raw_input ("Please select" 1--3 "")
if opt = = ' 1 ':
ret = User_register ()

Python Small note 1

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.