day08 python文件操作

day08 python
 
一.文件操作
    1.文件操作的函数
        open(文件名, mode=模式, encoding=字符集)
 
    2.模式: r, w, a, r+, w+, a+, rb, wb, ab, r+b, w+b, a+b
        r: read()
f = open('num.py', mode='r', encoding='utf-8')
print(f.read(3))                #代表读 3 个字符, 单个英文是一个字符, 单个汉字也是一个字符
print(f.read())                 # 读, 所有, 大文件不行,要考虑到内存
f.close()
    
        r: readline()
f = open('num.py', mode='r', encoding='utf-8')
print(f.readline(),end='\n')             #一次读一行, 光标随之移动
print(f.readline(),end='\n')             #print()函数有个换行, 而且readline()也会把文本中的换行符读出来
print(f.readline().strip(),end='\n')     #去掉一个换行,去掉 readline()的, 原文中的空行打印不出来
print(f.readline(),end='')               #去掉一个换行, 去掉 print() 的
f.close()
    
        r: while
f = open('num.py', mode='r', encoding='utf-8')
while True:
    s = f.readline()
    if s != '':
        print(s,end='')
f.close()                                   #程序停在这, 一直等(文件有增加时, 会继续打印), 可做监控用
print('program is over')
    
        r: for
f = open('num.py', mode='r', encoding='utf-8')
for line in f:
    print(line,end='')                      #和raadline()一样, 一行一行读, 会把文本中的换行读出来
    
        r: 编码
f = open(r'C:\Users\THINKPAD\Desktop\bajie.txt', mode='r', encoding='gbk')      #当读window文件时(中国系统默认gbk)
print(f.read())                              #1. window文件路径用 r''
f.close()                                    #2. 有中文时,用什么编码,就用什么解码    # 无中文时无所谓
    
        w: write()
f = open('num.py', mode='w', encoding='utf-8')  #带w的(w, w+, w+b), 只要你操作了,就会先清空源文件
f.write('bajieaishuishui\n')                    #还有创建文件的功能: 当文件不存在时,就创建
f.write('bajieaishuishui\n')                    #一行一行写的时候, 和readline()一样, 要注意换行符的问题
f.flush()
f.close()
    
        a: write()
f = open('num.py', mode='a', encoding='utf-8')
f.write('bajieaishuishui\n')
f.close()
    
        rb, wb, ab:  b要和模式配合(b的意思是bytes), 处理的是非文本文件
f = open(r'C:\Users\THINKPAD\Desktop\tupian.jpg', mode='rb')    #处理的是二进制,用不了 encoding
e = open(r'C:\Users\THINKPAD\Desktop\tupiancp.jpg', mode='wb')
 
for line in f:
    print(len(line))        #一次读一行, 读了多少不固定, 不用管
    e.write(line)
 
f.close()
e.close()
    
        r+: 先读后写
r+ 依旧是最好用的读写同时存在的模式
f = open('num.py', mode='r+', encoding='utf-8')     #读写的顺序,
 
s = f.readline()                #r+ 模式(唯一和光标原则不相符的地方), 当你先读后写时, 不论你读了多少,写入时都在文件最后写
f.seek(3)                       #当然你可以强行改变光标的位置
f.write('八戒\n')
 
print(s)
f.close()
    
        r+: 先写后读
f = open('num.py', mode='r+', encoding='utf-8')
f.write('八戒')
s = f.read()                    #先写后读里面貌似也有问题, 先写的写到哪?
print(s)                        #按理说是写到开头, 但是你后面跟的读如果不是read()读全部, 那么他也是写到最后
f.close()
   
         w+: 基本不用, 因为遇到w就清空特性不好
f = open('num.py', mode='w+', encoding='utf-8')
 
f.write('八戒')           #写完之后, 光标在最后, 再读是没内容的
f.seek(0)                   #光标移到开头
s = f.read()
print(s)
 
f.close()
    
        a+: 不常用, 因为开始的时候光标就在末尾,麻烦
f = open('num.py', mode='a+', encoding='utf-8')
f.seek(0)
print(f.read())
 
f.close()
        r+b, w+b, a+b: 一个二进制的文件不太可能同时读写, 很少用
 
    3.常用的操作
        光标 seek()
f = open('num.py', mode='r', encoding='utf-8')
f.seek(0)                           #这里的数字代表的是字节 bytes, utf-8 的汉字要用 3 个
f.seek(0, 2)
print(f.read(1))                    #这里的数字代表的是字符
f.close()
 
#f.seek(offset,whence) # offset偏移量,用的是bytes 为单位; 正数:向前冲; 负数:没有  # whence 0:从开头 1:从当前 2:从末尾
        
        tell()
f = open('num.py', mode='r', encoding='utf-8')
 
f.seek(3)
print(f.tell())             #和f.seek()一样, 告诉你在哪个bytes 的位置
 
f.close()
       
        truncate() 截断
f = open('num.py', mode='w', encoding='utf-8')
 
f.write('八戒爱谁谁')
f.seek(6)
f.truncate()         #默认把光标之后的内容干掉; 如果给了参数 n , 则干掉 n 后面的
 
f.close()
 
二.文件的修改(比如字符串替换, 不支持直接改)
 
import os
 
with open('num.py', mode='r', encoding='utf-8') as f1, open('num.py.fuben', mode='w', encoding='utf-8') as f2:
    for line in f1:
        line = line.replace('爱','----*----')
        f2.write(line)                              #把改好的每行写到副本文件中
os.remove('num.py')                                 #把原来的文件删掉
os.rename('num.py.fuben','num.py')                  #把副本改名成原来的文件
 
三.简单日志处理
    日志格式及需求
1,八戒, 10086, sioon          #每一行搞成这样 {'id':'1','name':'bajie','phone':'10086','car':'sioon'}, 最后搞到一个列表里
2,悟空, 36600, stang
3,信息, 45890, itngl
    
    代码
ls = []
with open('num.py',mode='r',encoding='utf-8') as f:
    for line in f:
        dic = {}
        lst = line.strip().split(',')
        dic['id'] = lst[0]
        dic['name'] = lst[1]
        dic['phone'] = lst[2]
        dic['car'] = lst[3]
        ls.append(dic)
print(ls)
 
 
 
 
 
 
    练习: 
    求水仙花数
for num in range(100,1000):
    sum3 = 0
    num = str(num)
 
    for i in num:
        sum3 = sum3 + int(i)**3
 
    if sum3 == int(num):
        print('%s yes' % num)
    
    random模块
import random
 
print(random.randint(0,1))        #取随机数字, 可以猜到结尾
    
    搞7个随机数, 不重复
import random
 
s = set()                #利用集合的不重复的特性
 
while len(s) < 7:
    s.add(random.randint(1, 36))
 
print(s)
    
    两个变量的数据交换
a = 10
b = 5
 
a, b = b, a
 
print(a,b)
 
  
 
 
 
 
 
 

猜你喜欢

转载自www.cnblogs.com/aiaii/p/12088350.html
今日推荐