Python learning--Python file manipulation

1 file reading

First create a file in .txt format in my E drive folder, the file name is foo.
Next read the file.

1.1 Read the entire file

file = open('foo.txt','r') #此时file是一个全局变量
contents = file.read()
print(contents)
file.close()      #需要写colse函数
with open('foo.txt','r') as file: #可以自动关闭文件
    contents = file.read()
    print(contents)

#区别:1变量的范围
#     2文件是否自动关闭

1.2 Read line by line

with open('foo.txt','r') as file: #可以自动关闭文件,逐行读取
    for line in file: 
        print(line.rstrip())

1.3 Read into the list

with open('foo.txt','r') as file: #可以自动关闭文件,逐行读取
    lines = file.readlines()
for line in lines:
    print(line.rstrip())

lines is a list

2 file writing

with open('foo.txt','w') as file:
    file.write('I love python1!\n')
    file.write('I love python2!\n')
    file.write('I love python3!\n')

w permissions, overwrite the previous content.

with open('foo.txt','a') as file:
    file.write('I love python1!\n')
    file.write('I love python2!\n')
    file.write('I love python3!\n')

a permission, append content in it.

Read and write permission
R+ Do not create a new file, the file read and write pointer is at the beginning
W+ Create a new file, the read and write pointer is at the beginning, if the file exists, it will overwrite the content before the file
A+ Create a new file, the read and write pointer is at the end (because it is an append ), will not overwrite previous content

3 seek function and tell function

File_object.seek(offet, whence)
whence parameter:
0 means to calculate from the beginning
1 means to calculate from the current position
2 means to calculate from the end of the file as the origin

with open('foo.txt','w') as file:
    file.write('www.python.org')
with open('foo.txt','rb+') as file:
    file.seek(0,0)
    print(file.tell())
    file.seek(3,0)
    print(file.tell())
    file.seek(1,1)
    print(file.tell())
    file.seek(1,2)
    print(file.tell())

3 Actual combat uses local files as databases to implement user login scripts

myfile = open('foo.txt','a')
while True:
    account = input('please input your ID:')
    passwd = input('please input your passwd:')
    str1 = '%s:%s'%(account,passwd)  #格式化保存
    myfile.write(str1)
    myfile.flush()
    print('congratulations')
    break;
myfile.close()

The above code can enter the user information. But there are still some small issues that need to be optimized.

Login part script:

myfile = open('foo.txt','a')
while True:
    account = input('please input your ID:')
    if account == '':
        print('please input your ID again!')
        continue
    passwd = input('please input your passwd:')
    if passwd == '':
        print('please input your passwd again!')
        continue
    str1 = '%s:%s\n'%(account,passwd)  #格式化保存
    myfile.write(str1)
    myfile.flush()
    print('congratulations')
    break;
myfile.close()

This allows subroutines to wrap automatically and prevent empty input.

myfile = open('foo.txt','r')
islogin = 1
oklogin = 0

while True:
    if oklogin == 0:
        if islogin == 4:
            print("三次错误, 再见!")
            break
        else:
            account = input('please input your ID:')
            passwd = input('please input your passwd:')
            for str in myfile:
                file_account = str.split(':')[0] 
                file_passwd = str.split(':')[1]  
                file_passwd = file_passwd[:-1] #切片判断
                if file_account == account and file_passwd == passwd:
                    oklogin = 1
                    break
            islogin += 1
            continue
    else:
        print("congratulations") 
        break
myfile.close()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325799532&siteId=291194637