python3 写入文件_笔记||Python3之文件的读写

【文件的读模式】

文件的对象:文件的读写通过文件操作对象进行。

Python2  -----  File

Python3  -----  TextIOWrapper

文件的打开:

内置函数open(file,mode)   ---  打开文件,获取文件操作对象。

一定要有file文件路径(路径、文件名、文件格式)  否则报错。

fo = open(fileDir)   -----  只读

路径的写法:①绝对路径 ----- 从根目录开始的

②相对路径 ------ ./当前位置    ../ 上一级

fileDir = 'C:/test.txt'

fileDir = 'C:\\test.txt'

fileDir = r'C:\test.txt'  ---  取消转义

读模式:如果该文件不存在会报错!

fo = open(dileDir, 'r')   <==>  fo = open(fileDir)

fo.tell()   -------------- 从0开始    显示指针位置

fo.read(2)  ------------ 读2个字符

fo.read() -------------- 读全部内容

文件中的换行是2个长度  \n     'a\nb'  -----  字符串中是一个

fo.close()  --- 关闭文件

移动文件指针位置:seek()

0模式: --- 绝对位置模式  fo.seek(1,0)   从0开始  一般像txt、log        r

前提:python3 一定在'rb'模式下 ---- 二进制模式 --- 非文本文件(图片)

1模式:当前位置开始移动  fo.seek(移动的位数,模式1)  -----  如:fo.seek(-1, 1)        rb

移动的位数 正数:向后移  负数:向前移

2模式:  ---从尾部位置开始   fo.seek(-1,2)          rb

移动的位数  正数:向后移  负数:向前移

代码例子:

fileDir = 'C:/test.txt'

fo = open(fileDir, 'rb')     # 打开文件

print('读前',fo.tell())     # 显示指针当前位置

print(fo.read(2))            # 读2个字符

print('读后',fo.tell())    # 再次显示指针当前位置

fo.seek(2,2)                 # 移动向后移动两位

print(‘移动后’,fo.tell())  # 再一次显示指针当前位置

readline:  读取一行

1 - 该方法返回是print(type(fo.readline())) ---

2 - 文件指针会做相应的偏移

readlines:读取所有行

1 - 该方法返回的是print(type(fo.readlines())) ---

区别:① fo.read()   -------- 返回str

② fo.readlines() -------- 返回是list

2 - fo.read().splitlines()  --- 返回list  而且去掉换行符

=================================================================================

【文件写模式】:

写模式特点:可以新建文件,若文件不存在则会创建一个新文件;

如果写模式打开一个文件,所有的内容会被清空。

1 - fo = open(fileDir, 'w')

2 - 如果该路径下的文件存在 --- 会清空

3 - 如果该路径下的文件不存在 ---- 会新建

4 - 在pycharm里面,你执行了fo.write('123') ----- 可以直接写进去

5 - fo.write('123') --- 返回值  ---- 写的字符长度

6 - fo.flush()  强行写入文件

7 - fo.close()   关闭文件会强行写入文件

追加模式 a:只是为了在文件末尾追加内容而打开文件

with open方式:

1 - with open(fileDir) as rFile:    ------等价于---------   rF = open(fileDir)

2 - 可以省略fo.close()

3 - 操作多个文件

seek:1 - (1,模式) ---  0模式 ---- 绝对位置,从0开始

2 - (1,模式) --- 1模式 ---- 当前位置,开始

3 - (1,模式) --- 2模式 ---- 尾部位置,开始

例子:

fileDir2 = open('C:/text.txt', 'w')

fileDir2.write('123456')   # 一定是str

# 如果写入时,需要换行时,应该怎么做?

fileDir2.write('01-test'+'\n')

fileDir2.write('02-test')

fileDir2.flush()    # 写入

fileDir2.close()   # 关闭  同样也会保存

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Python 3.11中,可以使用以下代码来读写文件: 读取文件: ```python # 以读模式打开文件 f = open('readme.txt', 'r', encoding='utf8') # 读取文件内容 con = f.read() # 关闭文件 f.close() # 显示内容 print(con) ``` 这段代码首先使用`open`函数以读模式打开文件,然后使用`read`方法读取文件内容,最后使用`close`方法关闭文件。读取的内容可以通过`print`语句显示出来。请注意,读模式和写模式不能混用,否则会导致错误。 写入文件: ```python # 以写模式打开文件 f = open('output.txt', 'w', encoding='utf8') # 写入内容 content = "Hello, World!" f.write(content) # 关闭文件 f.close() ``` 这段代码首先使用`open`函数以写模式打开文件,然后使用`write`方法写入内容。写入的内容可以是字符串或其他可写入的对象。最后使用`close`方法关闭文件。请注意,写模式会清空原有文件内容并且覆盖写入新的内容。 总结: 在Python 3.11中,可以使用`open`函数以指定的模式打开文件,并使用`read`和`write`方法读取和写入文件内容。读模式用于读取文件内容,写模式用于写入文件内容。根据需要选择适当的模式来读写文件即可。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Python笔记.docx](https://download.csdn.net/download/bxy0806/88235414)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [python 文件读写操作 200311](https://blog.csdn.net/whalecode/article/details/104792989)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [2022-10-24将要发布的python3.11比3.10快10%到60%](https://blog.csdn.net/zhaiminlove/article/details/126966986)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值