10、文件处理

一、文件打开模式

with open(r'a.txt',mode='wt',encoding='utf-8') as f:
   f.write("你好")

with open(r'a.txt',mode='ab') as f:
   f.write("哈哈哈".encode('utf-8'))


with open(r'a.txt',mode='rb') as f:
   data = f.read()
   print(data.decode('utf-8'))


with open(r'1.mp4',mode='rb') as f:
   data = f.read()
   print(data)

   for line in f:
       print(line)

''
with open('1.mp4',mode='rb') as f1,open(r'D:\111111111111.mp4',mode='wb') as f2:
   data = f1.read()
   f2.write(data)
   for line in f1:
       f2.write(line)

 

二、可读可写模式

with open('a.txt',mode='r+t',encoding='utf-8') as f:
   f.write('次卧')
   f.write('h')

 

三、修改文件的两种方式

with open('123.txt',mode='wb') as f:
  pass

with open('123.mp4',mode='rb') as f1,open('456.mp4',mode='wb') as f2:
  for line in f1:
      f2.write(line)

with open('123.txt',mode='rt',encoding='utf-8') as f1:
  date = f1.read()
  res = date.replace('wyf','WYF')
with open('123.txt',mode='wt') as f1:
  f1.write(res)
import os

with open('123.txt',mode='rt',encoding='utf-8') as f1,open('.123.txt.swp',mode='wt') as f2:
  date = f1.read()
  res = date.replace('WYF','wyf')
  f2.write(res)
os.remove('123.txt')
os.rename('.123.txt.swp','123.txt')


inpu_user = input("请输入你的用户名:")
inpu_pwd = input("请输入你的密码:")
inpu_money = input("请输入你的余额:")
with open('123.txt',mode='at') as f:
  f.write('%s:%s:%s\n' %(inpu_user,inpu_pwd,inpu_money))


import os

with open('123.txt',mode='rt',encoding='utf-8') as f1,open('.123.txt.swp',mode='wt') as f2:
  for line in f1:
      user,pwd,money = line.strip("\n").split(":")
      if user == "wyf":
          money = int(money)-100
      elif user == "cxk":
          money = int(money)+100
      line = '%s:%s:%s\n'%(user,pwd,money)
      f2.write(line)

os.remove('123.txt')
os.rename('.123.txt.swp','123.txt')

with open("198.txt",mode="wt") as f:
  print(f.writable())
  f.write('hcuwehfui')

with open("198.txt",mode='rt',encoding='utf-8') as f:
  print(f.readable())
  print(f.readline())

 

四、练习(注册和转账)

注册

inp_user = input("请输入您的用名:").strip()
inp_pwd = input("请输入您的密码:").strip()
inp_money = input("请输入您的余额:").strip()

with open('db.txt', mode='at', encoding='utf-8') as f:
   f.write('%s:%s:%s\n' % (inp_user, inp_pwd, inp_money))



转账
import os

with open('db.txt',mode='rt',encoding='utf-8') as f1,open('.db.txt.swp',mode='wt',encoding='utf-8') as f2:
   for line in f1:
       user,pwd,money = line.strip('\n').split(':')
       if user == 'tom':
           money = int(money) - 100
       elif user == 'lili':
           money = int(money) + 100

       line = "%s:%s:%s\n" % (user, pwd, money)
       f2.write(line)

os.remove('db.txt')
os.rename('.db.txt.swp','db.txt')

 

五、文件读写的其他方法

with open('b.txt',mode='rt',encoding='utf-8') as f:
   print(f.readable())   是否可读
   print(f.readline())   读取一行内容,光标移动到第二行首部
   print(f.readlines())  读取每一行内容,存放于列表中

   lines = f.readlines()
   print(lines[0])


with open('b.txt', mode='wt', encoding='utf-8') as f:
   print(f.writable())

   lines = ['你好\n', '我好\n', '大家好\n']

   for line in lines:
       f.write(line)
   f.writelines(lines)

   f.write('1111\n222\n333\n')


   f.write('hello')
   f.writelines('hello')

   f.flush()     立刻将文件内容从内存刷到硬盘



with open('b.txt', mode='at', encoding='utf-8') as f:
   f.truncate(3)    

 

六、控制文件指针移动

储备知识:只有t模式下的read(n)的n代表的是字符个数,除此以外都代表字节个数
with open('b.txt',mode='rt',encoding='utf-8') as f:
   print(f.read(4))    从第4个字符的位置读到文件末尾


with open('b.txt',mode='rb') as f:
   print(f.read(3).decode('utf-8'))


f.seek(移动的字节个数,模式)
模式:
0:参照文件开头,只有0模式可以在t下使用,其他的都只能在b下使用
with open('b.txt',mode='rt',encoding='utf-8') as f:
   f.seek(3,0)
   f.seek(6,0)
   print(f.tell())   查看当前指针距离文件开头的位置

1:参照指针当前所在的位置,只能在b下使用
with open('b.txt',mode='rb') as f:
   f.seek(3,1)
   f.seek(6,1)
   print(f.tell())

2:参照指针文件末尾,通常是倒着移动,只能在b下使用
with open('b.txt',mode='rb') as f:
   f.seek(-5,2)
   f.seek(-8,2)
   print(f.tell())
   print(f.read().decode('utf-8'))

 

上一篇:ubuntu下直接使用sqlite3


下一篇:mysql 1067 ERR] 1067 - Invalid default value for