python基础语法 007 文件操作-2文件支持模式&文件的内置函数

1.3 文件支持的模式

|-----|-------------------------------------------------------------------------------------------------------------------------------------|
| 模式 | 含义 |
| 'r' | open a file for reading(default) |
| 'w' | open a file for writing,creates a new file if it does not exist or truncates the file if it exists |
| 'x' | open a file foe exclusive creation. if the file already exists, the operation fails.独创模式,已经存在文件还写? 错误信息:fileexistserror:file wxists |
| 'a' | open for appending to the end of the file without truncating it ,creates a new file if it dose not exist |
| 't' | open in text mode (default) |
| 'b' | open in binary mode |
| '+' | open a file for updating (reading and writing) |

A、python中的open函数没有'rw' 这个参数,如果需要又读又写,可以使用 'r+' or 'w+' 来代替,否则会报下列错误

python 复制代码
file = open('new1_file', 'rw',encoding='utf-8')
file.write("url:/futureloan/mvc/api/member/register@mobile:18866668888@pwd:123456\n")
file.write("url:/futureloan/mvc/api/member/recharge@mobile:18866668888@amount:1000\n")
#读取数据
date = file.read()
file.close()



"""
ValueError: must have exactly one of create/read/write/append mode
"""

""" 改为:
file = open('new1_file', 'r+',encoding='utf-8')
"""

1.4 文件内置函数

1.4.1 readlines:

指:按行读取一个文本文件中的内容,并将每一行存储为一个字符串元素,最终返回一个包含所有元素的列表

python 复制代码
file = open('new1_file', 'r+',encoding='utf-8')
file.write("url:/futureloan/mvc/api/member/register@mobile:18866668888@pwd:123456\n")
file.write("url:/futureloan/mvc/api/member/recharge@mobile:18866668888@amount:1000\n")
data = file.readlines()
print(data)
file.close()


"""结果
['url:/futureloan/mvc/api/member/register@mobile:18866668888@pwd:123456\n', 'url:/futureloan/mvc/api/member/recharge@mobile:18866668888@amount:1000\n']

"""
python 复制代码
f = open("demo.txt",'w+',encoding='utf-8')
f.write('new line\n')
f.write('new2 line\n')
f.close()

"""
readlines() 读取每一行,会存放到列表当中,每一行的内容就是列表的一个元素
read() 得到的是一整个字符串
"""
f = open("demo.txt",'r',encoding='utf-8')
data = f.readlines()
print(data)

"""结果
['new line\n', 'new2 line\n']
"""

每个元素的末尾会存在\n, 怎么去除?

python 复制代码
"""
每个元素的末尾会存在\n

#分行打印时会把\n打印,
"""
#方法一
for line in data:
    print(line)

"""结果
new line

new2 line

"""

#方法二 去除\n
#strip 可以去除空格和换行
for line in data:
    print(line.strip())

"""结果
new line
new2 line
"""

#方法三 去除\n
for line, value  in enumerate(data):
    if line == len(data) -1:
        #最后一行
        print(value[:])
    else:
        #其他行
        print(value[:-1])

"""结果
new line
new2 line
"""

f.close()

1.4.2 tell / seek

指控制文件光标的操作

seek(1,2) :1表示光标移动的偏移量, 2表示相对的位置:0表示开始,1表示当前光标,2表示末尾

以字节来移动

1.4.3 with

防止忘记关闭文件

python 复制代码
"""
with 语句可以节省关闭文件的操作
"""
f = open("demo.txt")
f.read()
f.close()  # 该步骤容易遗忘

#打开文件,用f去接受
#上下文表达式
with open("demo.txt" ) as f:
    f.read()


#防止这样的书写
# f = open("demo.txt")
# f.read()
# f = open("demo.txt", 'w')
# f.write('hellp')
# f = open("demo.txt")
# f.read()

1.5 例子:

1.5.1 将文件中的内容读取

python 复制代码
#第一步打开文件
f = open("new_file.txt",encoding='utf-8')
data = f.read()
print(data)


#换行符分割 '\n'
new_data = data.split('\n')
print(new_data)
相关推荐
小珑也要变强37 分钟前
队列基础概念
c语言·开发语言·数据结构·物联网
AI原吾3 小时前
掌握Python-uinput:打造你的输入设备控制大师
开发语言·python·apython-uinput
机器视觉知识推荐、就业指导3 小时前
Qt/C++事件过滤器与控件响应重写的使用、场景的不同
开发语言·数据库·c++·qt
毕设木哥3 小时前
25届计算机专业毕设选题推荐-基于python的二手电子设备交易平台【源码+文档+讲解】
开发语言·python·计算机·django·毕业设计·课程设计·毕设
珞瑜·3 小时前
Matlab R2024B软件安装教程
开发语言·matlab
weixin_455446173 小时前
Python学习的主要知识框架
开发语言·python·学习
孤寂大仙v3 小时前
【C++】STL----list常见用法
开发语言·c++·list
D11_4 小时前
Pandas缺失值处理
python·机器学习·数据分析·numpy·pandas
花生了什么树~.4 小时前
python基础知识(四)--if语句,for\while循环
python
她似晚风般温柔7894 小时前
Uniapp + Vue3 + Vite +Uview + Pinia 分商家实现购物车功能(最新附源码保姆级)
开发语言·javascript·uni-app