Home > Backend Development > Python Tutorial > Introduction to basic operations of Python files

Introduction to basic operations of Python files

不言
Release: 2018-09-25 16:12:11
Original
2235 people have browsed it

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.

File processing process

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

Basic operation

Open file

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")
Copy after login

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

  • ##wb or w b

  • xb or w b

  • ab or a b

Note: When opening in b mode, the content read is of byte type. When writing, the byte type also needs to be provided and cannot be specified. Encoding

Read file content

# 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)
Copy after login

Close file

f.close()

#   一个完整的过程
try:
    f1 = open(path,"r", encoding="gbk")
    print(f1.read())
finally:
    if f1:
        f1.close()
Copy after login
with open(path,"r",encoding="gbk") as f2:   
print(f2.read())
Copy after login

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")
Copy after login
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)
Copy after login
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))
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template