Home > Article > Backend Development > Introduction to basic operations of Python files
This article brings you an introduction to the basic operations of Python files. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
1. Open the file, get the file handle and assign it to a variable
2. Operate the file through the handle
3. Close the file
r mode, the default mode, if the file does not exist, an error will be reported
w mode, if the file does not exist, it will be created, if the file exists, it will be overwritten
a mode, the file does not exist If the file exists, it will not be overwritten. The writing content will be written in the append mode (commonly used when writing log files). The append mode is a special writing mode
b(rb,wb,ab) Mode: No need to add encoding:utf-8
open(path, flag[, encoding][, errors]) path:要打开文件的路径 flag:打开文件的方式 r 以只读的方式打开文件,文件的描述符放在文件的开头 rb 以二进制格式打开一个文件用于只读,文件的描述符放在文件的开头 r+ 打开一个文件用于读写,文件的描述符放在文件的开头 w 打开一个文件只用于写入,如果该文件已经存在会覆盖,如果不存在则创建一个新文件 wb 打开一个文件只用于写入二进制,如果该文件已经存在会覆盖,如果不存在则创建一个新文件 w+ 打开一个文件用于读写,如果该文件已经存在会覆盖,如果不存在则创建一个新文件 a 打开一个文件用于追加,如果文件存在,文件描述符将会放在文件末尾 a+ encoding:编码格式 errors:错误处理 path = r"C:\Users\Desktop\file1.txt"#ignore 忽略错误f = open(path, "r",encoding="gbk")
The modes for opening files are:
r, read-only mode [default mode, the file must exist, an exception will be thrown if it does not exist]
w, write-only mode [unreadable; create if it does not exist; clear the content if it exists ]
x, write-only mode [unreadable; create if it does not exist, report an error if it exists]
a, append mode [readable ; If it does not exist, create it; if it exists, only append the content】
" " means that a file can be read and written at the same time
r, read and write [readable, writable]
w, write read [readable, writable]
x, write read [read, readable, Writable]
a, write and read [readable, writable]
"b" means operating in byte format
rb or r b
# 1、读取文件全部内容 str1 = f.read() print(str1) # 2、读取指定字符数 str2 = f.read(10) print("*"+str2+"*") str3 = f.read(10) print("*"+str3+"*") # 3、读取整行,包括"\n"字符 str4 = f.readline() print(str4) str5 = f.readline() print(str5) # 4、读取指定字符数 str6 = f.readline(10) print(str6) # 5、读取所有行并返回列表 list7 = f.readlines() print(list7) # 6、若给定的数字大于0,返回实际size字节的行数 list8 = f.readlines(20) print(list8) # 7、修改描述符的位置 f.seek(10) str9 = f.read() print(str9)
# 一个完整的过程 try: f1 = open(path,"r", encoding="gbk") print(f1.read()) finally: if f1: f1.close()
with open(path,"r",encoding="gbk") as f2: print(f2.read())Write file
path = r"C:\Users\Desktop\file4.txt" f = open(path,"w") # 1、将信息写入缓冲区 f.write("lee is a good man") # 2、刷新缓冲区 # 直接把内部缓冲区的数据立刻写入文件,而不是被动的等待自动刷新缓冲区写入 f.flush() f.write("lee is a good man\n") f.close() with open(path,"a") as f: f.write("good man")
with open('a.txt','w') as f: pass with open('a.txt','r') as read_f,open('b.txt','w') as write_f: data=read_f.read() write_f.write(data)
path = r"C:\Users\yanji\Desktop\day7\test1" with open(path,"wb") as f3: str = "lee is a good man" f3.write(str.encode("utf-8")) with open(path,"rb") as f2: data = f2.read() print(data) print(type(data)) newDate = data.decode("utf-8") print(newDate) print(type(newDate))
The above is the detailed content of Introduction to basic operations of Python files. For more information, please follow other related articles on the PHP Chinese website!