IO file series in Python built-in functions open and OS

Source: Internet
Author: User


This article describes the open and OS basic usage in IO.

This time with a game landing basic interface to do introduction, to introduce.

There are many ways to implement storage. For example, the dictionary and list, but the dictionary, the list is temporary, play a web game once or can, if it is a repeated use of a software, it is obviously inappropriate, more familiar with the media has the "file", for the program, the number of files stored in the file is not a good choice. Here is the concept of a database.

this time with files to store .

This time content Implement Login Verification

Login Verification form, you must enter the correct user name and password before you can log in

Verify three times altogether. Run the program successfully, as long as the validation is passed.

form: Secret game software needs to know the user name and password, if not know, three times have failed, the software locked. Will not open the software.

There are a lot of things you can do after you have a file, just arrange your storage properly,

Start registration, the registration name password stored to the file, and then log in to verify the time, from the password file inside, the user password read out, to compare, see whether the user input is the same, if there is to start playing this game. If you enter the user name, password three times, if it fails, the user name is locked. Verify that three unsuccessful times, lock the software, and you won't be able to run it again.

▲ Login Verification

? Open ()

? OS

Verify the form and must know the authentication name and password. Before you can start to run this program, to play this game, if three times are wrong, directly locked, next time will not open.

use two functions Poen (), OS

Open: Function: Write information to create a file to read content

Os: Function: System operation, determine whether the file exists.

General Cycle Judgment Example

"Heroes bate-0.2-1 Hero Test" #普通判断版本welcome = Print (' Welcome to Heroes world!--Welcome to the world of heroes! ') i = 0while True:username = input (' login--registered user name: ') password = input (' password--Enter your password: ') i + = 1 if user    Name = = ' Wang Go ' and password = = ' Administrator ': print (' < premium King VIP user > ') print (' ==loked login success = = ') break elif i = = 3:print (' Please enter the correct administrator password, you have been locked by loked: ')


Run Results

Enter the correct user password display

Supplemental While loop

Open and Os methods with IO file functions

To create a file, create a file with open. will show more beautiful.

The following is not print loked, is written by the Ope OS.

The following idea is to create a record information file, if there is this file, then I think that the software is locked, if there is no such file, then continue to enter the user name and password.

Later upgrade, then judge there is no this file, if there is this file, the inside record, a person's name, if recorded, then tell the account is locked. New Year's Day holiday, try to learn to fill up.



Open OS Use the

"' heroes bate-0.2-1  hero Beta ' #IO内置函数   open os  version import oswelcome =  print  (' welcome to heroes world! --  Welcome to the world of heroes! ') i = 0while true:    if os.path.isfile (' Lock.log '):         print (' locked--lock status ')         break     username = input  (' login--registered user name: ')     password =  input  (' password--Enter your password: ')     i += 1    if  username ==  ' Wang Go '  and password == ' Administrator ':          pass    else:        if i  == 3:            open (' Lock.log ', ' W '). Write (username)       &Nbsp;     print (' locked by-is it locked  %s '%username)              break        continue     print (' < senior Wang VIP Brother User > ')     print (' ==loked landing success, welcome to a 999-level hero Invincible World ==')


Run Results

Create a file and write the last user name

Entering the correct user password will enter the game running program. Running three times here is the exit loop condition. Didn't write anything else.

Operating status after lockout

Small description

Open is used to create a file.

Open (' Lock.log ', ' W '). Write (username)

Open a file named (' lock.log suffix file '), W is write, call method write information, write user name file record last input name.

/ If you don't have this file, create one yourself.

Print (' Locked by%s '%username)

Interpretation: Tell is locked by that user.

In order to determine whether the creation of a stored data file Lock.log exists.

This is the time to use a module, OS , in order to determine whether the file exists.

Judging the file is a method of existence. There is a class in the OS, there is a os.path- class . The Isfile-method determines whether there is a file, a file is not a file, use it to determine whether (' Lock.log ') exists. The subsequent destruction of the cycle, the judgment is established, directly into force.

The Unlock method deletes the files created in the Run directory.

Let the judgment lapse.

The contents of this chapter will be updated during New Year

This chapter harvest

IO an open built-in function in the file is used

A os.path.isfile in an OS class is used

. year December 29 Wang Yulin


IO file series in Python built-in functions open and OS

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.