#####Python(八)文件#####

####文件####
1.文本文件:可以处理各种语言所需的字符,只包含基本文字字符,不包含诸如字体、字号、颜色等信息
2.二进制文件:每一种二进制文件都需要自己的处理器才能打开
###文件的访问####
1.文件取读三部曲:打开---->操作---->关闭

###打开文件
f= open('/tmp/passwd')  ##写绝对路径
print(f)
##操作(读写权限)
content = f.read()   ##读文件
print(content)
f.write('hello')   ##写文件
print(content)
##关闭
f.close()

2.文件取读模式
r:
只能读,不能写;
读取的文件不存在时会报错;
FileNotFoundError: [Errno 2] No such file or directory: ‘/tmp/westos’

存在/tmp/passwd时
###打开文件
f= open('/tmp/passwd')  ##写绝对路径
print(f)
##操作
content = f.read()   ##读文件
print(content)
##关闭
f.close()
#可以看到文件内的内容
不存在的目录(没有/tmp/westos文件):
###打开文件
f= open('/tmp/westos')  ##写绝对路径  
print(f)
##操作
content = f.read()   ##读文件
print(content)
##关闭
f.close()
运行结果:
/usr/local/python3/bin/python3 /home/kiosk/PycharmProjects/python学习/文件.py
Traceback (most recent call last):
  File "/home/kiosk/PycharmProjects/python学习/文件.py", line 7, in <module>
    f= open('/tmp/westos')  ##写绝对路径
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/westos'

Process finished with exit code 1
执行写的权限会报错:
f= open('/tmp/passwd')  ##写绝对路径
print(f)
##操作
content = f.read()   ##读文件
print(content)
f.write('hello')   ##写文件
print(content)
##关闭
f.close()
运行:
Traceback (most recent call last):
  File "/home/kiosk/PycharmProjects/python学习/文件.py", line 12, in <module>
    f.write('hello')   ##写文件
io.UnsupportedOperation: not writable  ##报错:没有写的权限

r+:
可以执行读写操作;
文件不存在时,报错;FileNotFoundError: [Errno 2] No such file or directory: ‘/tmp/westos’

默认情况下从文件指针位置开始写入;

文件存在:
f= open('/tmp/passwd','r+')  ##写绝对路径
print(f)
##操作
content = f.read()   ##读文件
print(content)
f.write('hello')   ##写文件
print(content)
##关闭
f.close()
运行结果:可以看到写入的内容
[root@foundation46 tmp]# cat passwd  
harry:x:2016:2016::/home/harry:/bin/bash
sysadms:x:2017:2015::/home/sysadms:/bin/bash
materials:x:2018:2018::/home:/bin/bash
hello   #写入的内容

文件不存在:
f= open('/tmp/westos','r+')  ##写绝对路径
print(f)
##操作
content = f.read()   ##读文件
print(content)
f.write('hello')   ##写文件
print(content)
##关闭
f.close()
运行结果:
/usr/local/python3/bin/python3 /home/kiosk/PycharmProjects/python学习/文件.py
Traceback (most recent call last):
  File "/home/kiosk/PycharmProjects/python学习/文件.py", line 7, in <module>
    f= open('/tmp/westos','r+')  ##写绝对路径
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/westos'

Process finished with exit code 1

w:
只可以写不可以读,会覆盖原文件内容;
文件不存在会创建新的文件,

文件存在:
###打开文件
f= open('/tmp/passwd','w')  ##写绝对路径
print(f)
##操作
content = f.read()   ##读文件
print(content)
f.write('hello')   ##写文件
print(content)
##关闭
f.close()
运行:
Traceback (most recent call last):
  File "/home/kiosk/PycharmProjects/python学习/文件.py", line 10, in <module>
    content = f.read()   ##读文件
io.UnsupportedOperation: not readable   #不可读
<_io.TextIOWrapper name='/tmp/passwd' mode='w' encoding='UTF-8'>
###打开文件
f= open('/tmp/passwd','w')  ##写绝对路径
print(f)
##操作
f.write('hello')   ##写文件
##关闭
f.close()
运行:
<_io.TextIOWrapper name='/tmp/passwd' mode='w' encoding='UTF-8'>
[root@foundation46 tmp]# cat passwd 
hello  ##添加的内容,覆盖了源文件

文件不存在:
f= open('/tmp/westos1','w')  ##写绝对路径
print(f)
##操作
f.write('hello')   ##写文件
##关闭
f.close()
运行:
<_io.TextIOWrapper name='/tmp/westos1' mode='w' encoding='UTF-8'>  ##没报错
[root@foundation46 tmp]# cat westos1
hello

w+:
可读写,会清空文件原内容;
文件不存在时不会报错;

文件存在:
###打开文件
f= open('/tmp/passwd','w+')  ##写绝对路径
print(f)
##操作
content = f.read()   ##读文件
print(content)
f.write('hello')   ##写文件
print(content)
##关闭
f.close()
运行结果:
<_io.TextIOWrapper name='/tmp/westos4' mode='w+' encoding='UTF-8'>

文件不存在
f= open('/tmp/westos4','w+')  ##写绝对路径
print(f)
##操作
content = f.read()   ##读文件
print(content)
f.write('hello')   ##写文件
print(content)
##关闭
f.close()
运行结果:
<_io.TextIOWrapper name='/tmp/westos4' mode='w+' encoding='UTF-8'>

a:
只可写不可读,不会覆盖原文件的内容;
文件不存在不会报错

文件存在
##打开文件
f= open('/tmp/passwd','a')  ##写绝对路径
print(f)
##操作
content = f.read()   ##读文件
print(content)
f.write('hello')   ##写文件
print(content)
##关闭
f.close()
运行:
Traceback (most recent call last):
  File "/home/kiosk/PycharmProjects/python学习/文件.py", line 10, in <module>
    content = f.read()   ##读文件
io.UnsupportedOperation: not readable  #不可读
<_io.TextIOWrapper name='/tmp/passwd' mode='a' encoding='UTF-8'>

文件不存在:
###打开文件
f= open('/tmp/westos6','a')  ##写绝对路径
print(f)
##操作
f.write('hello')   ##写文件
##关闭
f.close()
运行:
<_io.TextIOWrapper name='/tmp/westos6' mode='a' encoding='UTF-8'>


a+:
可读写,不会覆盖原文件的内容;
文件不存在不会报错

文件存在
###打开文件
f= open('/tmp/passwd','a+')  ##写绝对路径
print(f)
##操作
content = f.read()   ##读文件
print(content)
f.write('hello')   ##写文件
print(content)
##关闭
f.close()
运行:
<_io.TextIOWrapper name='/tmp/passwd' mode='a+' encoding='UTF-8'>
[root@foundation46 tmp]# cat passwd
hellohellohello  #不会覆盖源文件

文件不存在:
f= open('/tmp/westos5','a+')  ##写绝对路径
print(f)
##操作
content = f.read()   ##读文件
print(content)
f.write('hello')   ##写文件
print(content)
##关闭
f.close()
运行:
<_io.TextIOWrapper name='/tmp/passwd' mode='a+' encoding='UTF-8'>

f.readable()# 判断文件对象拥有的权限
f.writable( )# 判断文件对象拥有的权限
f.tell( ) #告诉当前的文件所在的位置

f= open('/tmp/passwd','r+')  ##写绝对路径
print(f)
##操作
content = f.read()   ##读文件
print(content)
print(f.tell())  #告诉当前的文件所在的位置
f.write('hello')   ##写文件
print(content)
print(f.tell())  #告诉当前的文件指针所在的位置
print(content)
print(f.readable()) # 判断文件对象拥有的权限
print(f.writable())  # 判断文件对象拥有的权限
##关闭
f.close()
运行:
<_io.TextIOWrapper name='/tmp/passwd' mode='r+' encoding='UTF-8'>
hellohellohellohellohello
25  ##当前的文件指针所在的位置
hellohellohellohellohello
30  #当前的文件指针所在的位置
hellohellohellohellohello
True  #可读
True  #可写

###非文本文件的取读###
如果读取是 图片 音频 视频(非纯文本文件)
需要通过二进制的方式读取和写入
-读取纯文本
r r+ w w+ a a+ 等同于 rt rt+ wt wt+ at at+
-读取二进制文件
rb rb+ wb wb+ ab ab+

先读取二进制文件内容,保存在变量content里

 f1 = open('today.png',mode='rb')
content = f1.read()
f1.close()
print(content)

运行:
在这里插入图片描述
二进制文件的复制

f2 = open('lucky.jpg',mode='wb')  #打开要写入的文件
 写入要复制的文件的内容
f2.write(content)  #保存在变量content里的内容
f2.close()

###文件的取读方法###
默认情况下读取文件的内容 小的文件:直接用read读取即可
如果是一个大文件(文件大小>=内存大小) readline()

按行读取 f.readline()

f = open('/tmp/passwd','r+')
f.readline() # 按行读取
print(f.readline())
f.close()
运行:
bin:x:1:1:bin:/bin:/sbin/nologin

按字节读取

 f = open('/tmp/passwd','r+')
 print(f.read(10))  ##读取10个字节
print(f.tell())  ##告诉指针的位置
f.close()
运行:
root:x:0:0
10

读取文件内容,并返回一个列表,列表元素分别为文件的行内容

 f = open('/tmp/passwd','r+')
 print(f.readlines())
f.close()

运行:
在这里插入图片描述
####指针位置的移动###
告诉当前指针位置f.tell( )


f = open('/tmp/passwd','r+')
print(f.tell())  ##文件读取前的指针位置
print(f.read(9))  #按字节读
print(f.tell())#文件读取后的指针位置
运行:

0
root:x:0:
9

Process finished with exit code 0

seek:指针移动
第一个参数:偏移量 >0:代表向后移动; <0 代表向前移动
第二个参数:
0:移动指针到文件开头
1:当前位置
2:移动指针到末尾
f.seek(0,0) #将文件指针移动到文件最开始
f.seek(0,2) #将文件指针移动到文件最后

f = open('/tmp/passwd','r+')
print(f.tell())  ##文件读取前的指针位置
print(f.read(9))  #按字节读
print(f.tell())#文件读取后的指针位置
f.seek(0,2)
print(f.tell())
运行:
0
root:x:0:
9
2539

读取内容,返回一个列表,去掉后面的\n ##(列表生成式,map)
f.readlines( ) #读取文件返回一个列表,元素为文件行的内容

f = open('/tmp/passwd')  
print(list(map(lambda x:x.strip(),f.readlines())))  #lambda  匿名函数
print([line.strip() for line in f.readlines()])
f.close()

创建文件data.txt 文件共100000行,每行存放以一个1~100之间的整数

import random
f = open('data.txt','a+')
for i in range(100000):
    f.write(str(random.randint(1,100)) + '\n')
#移动文件指针
f.seek(0,0)
print(f.read())
f.close()

###文件的安全取读with###
上下文管理器:打开文件,执行完with语句内容之后,自动关闭文件

f = open('/tmp/passwd')
with open('/tmp/passwd') as f:
    print(f.read())
    #print('with里面的语句:',f.close())
    #print(f.close())

print('after with语句',f.closed)

将第一个文件的内容写道第二个文件中
python3.x (python2.x 不支持这个写法)
with open(’/tmp/passwd’) as f1,open(’/tmp/westos’,‘w+’) as f2:
#将第一个文件的内容写到带二个文件
f2.write(f1.read()) #第二个文件可读可写,第一个文件可读
f2.seek(0,0) #将指针移动到文件最开始地方
print(f2.read())

python2.x 的写法:
with open(’/tmp/passwd’) as f1:
content = f1.read()
with open(’/tmp/westos’,‘w+’)as  f2:
f2.write(content)
“”"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值