Python-文件操作

概述

文件操作-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)

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值