The file operation process
- Open the file, get the file handle and assigned to a variable
- Operate on files handles
- Close the file
Basic Operations
#读文件
f = open("Oneday.txt","r", encoding="utf-8")
data=f.read() #read file
print(data,"\n")
f.close()
#写文件
f01 = open("Oneday01.txt","w", encoding="utf-8")
f01.write ( " time will have passed \ the n- " )
f01.write ( " Oriental \ the n- " )
f01.close()
# Additional content file
F01 = Open ( " Oneday01.txt " , " A " , encoding = " UTF-. 8 " )
f01.write("追加\n")
f01.write("一次!")
f01.close()
# The readline 5 reads the front row of content
F = Open ( " Oneday.txt " , " R & lt " , encoding = " UTF-. 8 " ) # filehandle
for i in range(5):
Print (f.readline ()) # line read file contents
# Cycle the entire contents of the file, read the contents of line 10 stops operation # List mode F = Open ( " Oneday01.txt " , ' R & lt ' , encoding = " UTF-. 8 " ) # filehandle for line in f.readlines(): Print (line.strip ()) # print the entire list F = Open ( " Oneday01.txt " , ' R & lt ' , encoding = " UTF-. 8 " ) # filehandle for index,line in enumerate(f.readlines()): if index == 9: Print ( " I was the first 10 lines, printing far! \ the n- " ) continue Print (line.strip ()) # print the entire list # non-scheduled manner COUNT = 0 for Line in F: IF COUNT ==. 9 : Print ( ' ----------------- - ' )
count += 1 continue print(line) COUNT +. 1 =
# generally use two or more non-scheduled mode
Common file operations:
Print (f.encoding) # encoded print file, using the side utf-8
Print (f.fileno ()) # Python API interface file out with a number of systems
Print (F.Name) # the name of the print file
print(f.seekable()) #判断指针是否可以进行移动,因为不是所有的文件类型都是可以移动的,可以移动返回True结果
print(f.readable()) #判断文件现在是否可以读
print(f.writable()) #判断文件现在是否可以写
print(f.flush()) #刷新内存缓存中的数据,以便于时时的将缓存存入到硬盘中
print(f.closed) #判断文件是否被关闭
print(f.encoding) #打印文件的编码,这边使用的是utf-8
print(f.fileno()) #python文件掉用系统的API接口的编号
print(f.name) #打印文件的名字
print(f.seekable()) #判断指针是否可以进行移动,因为不是所有的文件类型都是可以移动的,可以移动返回True结果
print(f.readable()) #判断文件现在是否可以读
print(f.writable()) #判断文件现在是否可以写
print(f.flush()) #刷新内存缓存中的数据,以便于时时的将缓存存入到硬盘中
f.flush()功能的解释说明
#说明:加上flush选项之后,#号循环一次打印一次,所以看到的结果是#号被逐渐打印出来,和就是flush刷新内存的作用
import sys,time
for i in range(20):
sys.stdout.write("#")
sys.stdout.flush()
time.sleep(0.1)
文件的其他模式:
f = open("Oneday01.txt",'r', encoding="utf-8") #文件句柄
print(f.tell()) #按照字符来进行计数
print(f.readline())
print(f.readline())
print(f.readline())
print(f.tell())
f.seek(0) #将指针移动到第一行来进行重新读取文件
print(f.readline()) #重新打印第一行内容
f = open("Oneday01.txt",'a', encoding="utf-8")
f.seek(10) #将光标移动到第10行
f.truncate(20) #从文件开头截断10个字符,包括空格 '''
#文件的读写模式,打开,并且可以追加数据(工作中常用的一种模式)
f = open("Oneday02.txt",'r+', encoding="utf-8")
print(f.readline())
print(f.readline())
print(f.readline())
f.write("------------------r+-----------")
print(f.tell())
print(f.readline())
#文件的写读模式,先创建一个文档,然后再写数据
f = open("Oneday02.txt",'w+', encoding="utf-8")
print(f.readline())
print(f.readline())
print(f.readline())
f.write("------------------w+-----------\n")
f.write("------------------w+-----------\n")
f.write("------------------w+-----------\n")
f.write("------------------w+-----------\n")
print(f.tell())
f.seek(10)
print(f.tell())
f.write("should be at the begining of the second line!")
print(f.readline())
#文件的追加读模式,可以追加读
#f = open("Oneday02.txt",'a+', encoding="utf-8")
二进制文件:
#文件的二进制格式,以二进制的模式读取文件
# 一般网络传输(FTP)的情景下会需要用到二进制格式,另外一种是例如类似的二进制视频文件场景需要用到这种模式
f = open("file01.mp4",'rb')
print(f.readline())
print(f.readline())
print(f.readline())
#二进制文件的写模式
f = open("Oneday02.txt",'wb')
f.write("hello binary01\n".encode()) #encode告送系统如果找不到字符类型的时候默认去找UTF-8模式,如果不写就会报str类型错误
f.close()
总结:
打开文件的模式有:
- r,只读模式(默认)。
- w,只写模式。【不可读;不存在则创建;存在则删除内容;】
- a,追加模式。【可读; 不存在则创建;存在则只追加内容;】
"+" 表示可以同时读写某个文件
- r+,可读写文件。【可读;可写;可追加】
- w+,写读
- a+,同a
"U"表示在读取时,可以将 \r \n \r\n自动转换成 \n (与 r 或 r+ 模式同使用)
- rU
- r+U
"b"表示处理二进制文件(如:FTP发送上传ISO镜像文件,linux可忽略,windows处理二进制文件时需标注)
- rb
- wb
- ab