python:文件操作___008

1、文件操作:

(1)文件的路径:相对路径或绝对路径

(2)编码方式:utf-8、gbk等

(3)操作方式:

只读r、rb;

只写w、wb; 

追加a、ab;

(注意:以什么编码方式存储文件,就以什么方式打开文件操作)

bytes通常用于网络数据传输、二进制图片和文件的保存等等 

2、文件操作:读 r , rb , r+  , r + b

(1)例子:r

f = open(r"C:\Users\Administrator\Desktop\cs.txt",mode='r',encoding='gbk')
file_ = f.read()   # 读取的过程中bytes---转换成功str
print(file_,type(file_))
f.close()

# <class '_io.TextIOWrapper'>
# 测试信息asldjf <class 'str'>   

(2)rb以二进制方式读取文件,并decode解码

例子:

f = open(r'C:\Users\Administrator\Desktop\cs.txt',mode='rb')
file = f.read().decode('gbk')
print(file,type(file))
f.close()

# 测试信息asldjf <class 'str'>

(3)r+ 读写,不能写读; r+b省略

例子:

f = open('file.txt',mode='r+',encoding='utf-8')
print(f.read())
f.write('cs')
f = open('file.txt',mode='r+',encoding='utf-8')
print(f.read())
f.close()

(4)w,wb写:先将原文件内容清除,再重新写入

1》例子:w

f = open('file.txt',mode='w',encoding='utf-8')
f.write('写入')
f = open('file.txt',encoding='utf-8')
file = f.read()
print(file,type(file))

2》例子2:wb  以二进制方式写入

f = open('file.txt',mode='wb')
f.write('测试'.encode('utf-8'))
f = open('file.txt',encoding='utf-8')
file = f.read()
print(file,type(file))  # 测试 <class 'str'>

3》w+ 写读,先删除之前写的,在写

f = open('file.txt',mode='w+',encoding='utf-8')
f.write('salfajs')
f.seek(0)  # 方法用于移动文件读取指针到指定位置。
print(f.read())

4》追加:a,ab;在文件内容的基础上,添加信息

例子:

f = open('file.txt','a',encoding='utf-8')
f.write('sdfsddf123')
f.close()



f = open('file.txt',mode='ab')
f.write('江河'.encode('utf-8'))
f.close()
f1 = open('file.txt',encoding='utf-8')
print(f1.read())

5》a+ 写读

例子:

f = open('file.txt',mode='a+',encoding='utf-8')
f.write('211')
f.seek(0)
print(f.read())
f.close()

3、文件读写模式总结:

r,r+,w,w+,a,a+区别

模式 可操作 若文件不存在

是否覆盖

之前内容

r 只能读 报错
r+ 可读写 报错

先写则覆盖,

先读不覆盖

w 只能写 创建新的 覆盖
w+ 可写读 创建新的 覆盖
a 只能写(追加) 创建新的
a+ 可写读 创建新的

4、功能详解

(1)read()    读出来的都是字符串

例子:即前三个字符

f = open('file.txt',mode='r+',encoding='utf-8')
count = f.read(10)
print(count)
f.close()

(2)tell()  告诉你光标的位置

f = open('file.txt',mode='r+',encoding='utf-8')
f.seek(3)  # 定位光标的位置
count = f.tell()  # 告诉你光标的位置
print(count)
f.close()

(3)seek() 定位光标的位置

(4)readline()  只读一行

f = open('file.txt',mode='r+',encoding='utf-8')
line = f.readline()  # 只读第一行
print(line)
f.close()

(5)readlines()   以列表形式,每一行当成列表中的一个元素,添加到列表当中

f = open('file.txt',mode='r+',encoding='utf-8')
line = f.readlines()
print(line)
print(line[2])
f.close()
#['salfajssdfsddf123测试江河江河211\n', '12345sdsfsdf\n', 'sdfsddfsddf232113']
#sdfsddfsddf232113

(6)常见的文件操作方法

f.readable()  是判断有读取文件的方法

f.read([size))  size为读取的长度,以byte为单位,返回从字符串中读取的字节 如:2,字符为2个,从第一个开始

f.readline([size]) 读取一行

f.readlines([size]) 返回一个list,其实内部是调用readline()实现的

f.write(str)  把str写到文件中,write()不主动加换行符

f.writelines(str)  把多行内容一次写入

f.close()  关闭文件

f.flush() 把缓冲区内容写入硬盘

f.tell() 当前文件操作标记的位置

f.next()  返回下一行,并将文件操作标记位移到下一行。把一个file用于for...in file这样的语句时,就是调用next() 函数来实现遍历的

(7)操作列表标准写法:可以操作多种

多个文件一起操作例子:

with open('file.txt',mode='r+',encoding='utf-8') as f,open('f1.txt',mode='r+',encoding='utf-8') as f1:
    for i in f:
        j = f1.read(3)
        print(i,j)

truncate() 方法用于截断文件,如果指定了可选参数size,则表示截断文件为size个字符。如果没有指定size,则从当前位置起截断,截断后面的所有字符被删除。

fileObject.truncate([size])  size可选,如果存在则文件截断为size字节

5、注册登录的例子:

# 注册
username = input('请输入注册的用户名:')
password = input('请输入注册的密码:')
with open('file1.txt',mode='w',encoding='utf-8') as f:
    f.write('{}\n{}'.format(username,password))

# 登录
li = []
i = 0
while i<3:
    usr = input('请输入用户名:')
    psd = input('请输入密码:')
    with open('file1.txt',mode='r',encoding='utf-8') as f:
        for j in f:
            li.append(j)
        if usr == li[0].strip() and psd == li[1]:

            print('登录成功')
            break
        elif i == 2:
            print('密码输入三次,无法输入')
        else:
            print('用户名或密码错误,请重新输入!')
        i += 1

8、

(1)编码:

str---->bytes  encode编码

(2)解码:

s = 'js'
s1 = s.encode('utf-8')
print(type(s1))  # <class 'bytes'>
s2 = s1.decode('utf-8')
print(type(s2))  # <class 'str'>

猜你喜欢

转载自blog.csdn.net/weixin_41253809/article/details/85097214
008