Python file operations,

Source: Internet
Author: User

Python file operations,

Several file opening modes

Open ('path') # open ('path', 'r + ') by default in read-only mode # open in read/write mode. If there is content, it overwrites the content of the corresponding string from the ground up ('path', 'w') # writes, overwrites the file, and writes again. If there is no file, create an open ('path', 'W + ') # read/write by yourself. The function is the same as that of open ('path', 'A') # Write, Append content at the end of the file, if the file does not exist, create open ('path', 'A' +) # read/write, same as above. The most common open ('path', 'B') # open a binary file open ('path', 'U') # support for all line breaks: Login Registration
#! /Usr/bin/env python # coding: rj8f1_open('user.txt ', 'a +') # single row write # f. write ("wd: 1234") # write multiple rows # names = ["pc: 123 \ n", "panda: 123 \ n"] # f. writelines (names) # interactive registration while True: name = raw_input ('Enter the User name :'). strip () password = raw_input ('enter your password :'). strip () repass = raw_input ('Enter the password again :'). strip () if len (name) = 0: print "the user name cannot be blank. Please enter it again !! "Continue; if len (password) = 0 or password! = Repass: print "Incorrect password" continue; else: print "congratulations, registration successful" break; f. write ("% s: % s" % (name, password) f. close ()

 

#! /Usr/bin/env python # coding: utf-8fo = open ("user.txt") '''num = 1 while True: line = fo. readline () # print repr (line) print "% s --> % s" % (num, line. rstrip ("\ n") num + = 1 if len (line) = 0: break ''' # Read all characters from the file, the dictionary dict1 ={} content = fo. readlines () # Save the lecture file result as a list fo. close () # print contentfor user in content: name = user. rstrip ("\ n "). split (":") [0] # print name dict1 [name] = user. rstrip ("\ n "). split (":") [1] # print dict1 # determine the user's account and password. All are OK and the logon is successful. Otherwise, the error count = 0 while True: count + = 1 if count> 3: print "Sorry, you have entered too many errors and your account is locked. Contact the Administrator "break name = raw_input ('enter User name: '). strip () if name not in dict1: print" the user name does not exist. Please enter it again !! "Continue; password = raw_input ('enter your password: '). strip () if password! = Dict1 [name]: print "Incorrect password input" continue; else: print "congratulations, login successful" break;

  

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.