概述
文件操作-open()
open()。是建立一个输入输出流。类似一根水管,数据就像水流一样从水管中流入流出。
格式
open(file, mode, buffering, encoding, errors, nerline, closefd)
常用格式
open(file_path, mode' ')
mode
'r' open for reading (default) 'w' open for writing, truncating the file first 'x' create a new file and open it for writing 'a' open for writing, appending to the end of the file if it exists 'b' binary mode 't' text mode (default) '+' open a disk file for updating (reading and writing) 'U' universal newline mode (deprecated)
默认模式就是'rt'。'读'模式打开'文本'格式文件。
先在E盘创建一个文本文件。E:/python练习/test-file.txt。以下所有举例,文件案的初始内容都是以下内容。
读文件-read
read
读取文件全部内容
open_file = open('E:/python练习/test-file.txt')
read_container = open_file.read()
print(read_container)
open_file.close()
-------------
输出:
file test1
file test2
readline
独去文件行内容。如果不做循环,只会读取第一行。
open_file = open('E:/python练习/test-file.txt')
read_line_continer = open_file.readline()
print(read_line_continer)
open_file.close()
-------------
输出:
file test1
可以使用循环输出所有内容。除了最后一行外的每一行都会自动加换行符。
open_file = open('E:/python练习/test-file.txt')
while True:
read_line_continer = open_file.readline()
if read_line_continer:
print(read_line_continer)
else:
break
open_file.close()
--------------
输出:
file test1
file test2
readlines
按行独去文件,将所有行放入list。除了最后一行外的每一行都会自动加换行符。
后续我们要读取的时候,操作list即可。
open_file = open('E:/python练习/test-file.txt')
read_lines_container = open_file.readlines()
print(read_lines_container)
open_file.close()
-----------
输出:
['file test1\n', 'file test2']
readable
判断文件是否可读。返回布尔类型。
open_file = open('E:/python练习/test-file.txt')
readable_container = open_file.readable()
print(readable_container)
open_file.close()
--------------
输出:
True
写文件-write
open(),使用mode 'w'进行文件写入,会覆盖原文件内内容。
open(),使用mode 'a'进行文件写入,可追加写入内容。
write
mode 'w'
open_file = open('E:/python练习/test-file.txt','w')
msg = '''
写入第一行
写入第二行
'''
open_file.write(msg)
open_file.close()
open_file = open('E:/python练习/test-file.txt')
read_container = open_file.read()
print(read_container)
open_file.close()
----------
输出:
写入第一行
写入第二行
mode 'a'
open_file = open('E:/python练习/test-file.txt', 'a')
msg = '''
写入第一行。
写入第二行。
'''
open_file.write(msg)
open_file.close()
open_file = open('E:/python练习/test-file.txt')
read_container = open_file.read()
print(read_container)
open_file.close()
------------
输出:
file test1
file test2
写入第一行。
写入第二行。
writeline
- writeline将list的内容写入文件;
mode 'w'
open_file = open('E:/python练习/test-file.txt', 'w')
msg_list = ['写入第一行\n', '写入第二行']
open_file.writelines(msg_list)
open_file.close()
open_file = open('E:/python练习/test-file.txt')
read_container = open_file.read()
print(read_container)
open_file.close()
-----------
输出:
写入第一行
写入第二行
mode 'a'
open_file = open('E:/python练习/test-file.txt', 'a')
msg_list = ['写入第一行\n', '写入第二行']
open_file.writelines(msg_list)
open_file.close()
open_file = open('E:/python练习/test-file.txt')
read_container = open_file.read()
print(read_container)
open_file.close()
-----------
输出:
file test1
file test2写入第一行
写入第二行
备注:“file test2” 和 “写入第一行”在同一行,是因为“file test2”结尾没有换行符。
writable
判断文件是否可写入
open_file = open('E:/python练习/test-file.txt', 'a')
writeable_container = open_file.writable()
print(writeable_container)
open_file.close()
open_file = open('E:/python练习/test-file.txt', 'r')
writeable_container = open_file.writable()
print(writeable_container)
open_file.close()
-----------
输出:
True
False
文件关闭
open操作建立输入输出流之后,使用完,我们需要释放资源
close
close()上述例子中已经用到很多。在对输入输出流操作完后,就调用close(0
with...as...
with...as... 也可以自动地帮助我们释放资源。这样就不用每一次调用close()
with open('E:/python练习/test-file.txt', 'r') as openfile:
read_container = openfile.read()
print(read_container)