python爬虫的文档基本写入与读取

'''文件上传
 保存log

系统函数:
 open(file,mode,buffering,encodeing)

 读:
   open(path/filename,'rt')---->返回值:stream (管道)

   container = stream.read()  ---->读取管道中内容

   注意: 如果传递的path/filename有误,则会报错:FileNotFoundError
    如果是图片则不能使用默认的读取方式,mode = 'rb'

    总结:
    read()  读取所有内容
    readline() 每次读取一行内容
    readlines() 读取所有的行保存到列表中
    readable()  判断是否可读的'''
file=open(r'C:\\Users\\86159\\Desktop\1.txt','rb')
print(file.readable())

container=file.readlines()
print(container)

for i in container :
    print(i)

# 写文件
'''
stream = open(r'c:\p1\aa.txt', 'w')
mode 是’w‘ 表示就是写操作  每次都会将原来的内容清空,

方法:
     write(内容)   然后写当前的内容
    writelines(Iterable)  没有换行效果
    stream.writelines(['赌神高进\n', '赌侠小刀\n', '赌圣周星星\n'])  ---》自己加

如果mode='a'


'''
file1=open(r'C:\\Users\\86159\\Desktop\1.txt','a')
container=file1.write('我最帅')
container=file1.writelines(['帅啊\n','太帅了\n'])
file.close()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值