pyton-异常、文件的读取、CSV,OS

1、测试写入中文

#1测试写入中文
f=open(r"c:/Users/FYL/Desktop/a.txt","w")
f.write("尚学堂\n百战程序员\n")
f.close()
#使用异常机制管理文件对象的关闭操作
try:
    f=open(r"e.txt","w")
    strs=["aa","cc","bb"]
    f.writelines(strs)
except BaseException as e:
    print(e)
finally:
    f.close()
# 2.文本文件的读取工作read,readline,readlines
#read
with open("e.txt","r",encoding="utf-8") as f:
    str=f.read()
    print(str)
with open("e.txt","r",encoding="utf-8") as f:
    for a in f:
        print(a,end="")
#3.enumerate
a=["我love u","尚学堂\n","百战程序员\n"]
b=enumerate(a)
print(a)
print(list(b))
#4.推导式
with open("e.txt","r",encoding="utf-8") as f:
    lines=f.readlines()
    lines=[line.rstrip()+" #"+str(index+1)+'\n' for index,line in enumerate(lines)]#推导式生成列表
print(lines)
with open("e.txt","w",encoding="utf-8")as f:
    f.writelines(lines)
#5,二进制文本的操作
with open("dog.jpg","rb") as f:
    with open("copy_dog.jpg","wb") as w:
        for lines in f.readlines():
            w.write(lines)
print("图片拷贝完成")
#6.文件对象的常用方法和属性
#seek
with open("e.txt","r",encoding="utf-8") as f:
    print("文件名是
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值