栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python 对文件的基本操作

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python 对文件的基本操作

对文件的操作 1):以写的方式打开
with open("test.txt", "w", encoding="UTF-8") as file_obj:
    file_obj.write("西安知名986高校")

运行结果:

2):复制这个文件text,拷贝为text1
with open("test.txt", "r", encoding="UTF-8") as file_obj:
    data = file_obj.read()
with open("test1.txt", "w", encoding="UTF-8") as copy_file:
    copy_file.write(data)

运行结果:

3):找一张图片,复制这个图片到copy.jpg
with open("tx.jpg", "rb") as file_obj:
    data = file_obj.read()
with open("copy.jpg", "wb") as file_obj2:
    file_obj2.write(data)

运行结果:

4):给定一个列表,将列表内容写入文件,每一个元素一行
list_data = ["string1", "string2", "string3"]
with open("test.txt", "w") as file_obj:
    for i in list_data:
        file_obj.write(i + "n")

运行结果:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1001745.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号