python入门第三天——文件/存储器

##python:input /output(I/O)

##很多时候,你会想要让自己的程序与用户交互,从用户
##得到输入,然后打印一些结果
##常用的是输入输出类型是处理文件,创建,读写文件的能力是
##必须的
##file:可以通过file类的对象来打开一个文件
##注意:对文件的读写能力依赖于程序在打开文件时的指定模式
##注意:当在完成对文件的一系列操作后,记住调用close()告诉python我们完成了对文件的使用
# print help('file')

poem = """
        Programing is fun
        when the work is done
        
        if you wanna make your work also fun
        use Python!"""

f = file("poem.txt",'w')
f.write(poem)
f.close()

my = file('poem.txt','r')##如果要对文件操作需要调用file(),默认操作是可读
while True:
    line = my.readline()
    if len(line) == 0:
        break
    print line

my.close()

##文件名也就是指针,指向文件的存储的内存
##读取文件时需要文件名来寻找file
# 文件的三种模式:可读(r),可写(w),追加(a)
# 文件的打印,readline()逐行来打印文件的内容

##存储器:pickle ,使用它,可以在一个文件中存储任何python对象
##之后依旧可以完成无缺的取出来,这被称为持久的存储对象
import pickle as p

filename = 'fruit'
List = ['apple','orange','banana']

f=file(filename,'w')
p.dump(List,f)
f.close()

del List #remove the list

##read back from the storage
f=file(filename)
storedList = p.load(f)
# while True:
#     line = f.readline()
#     if len(line) == 0:
#         break
#     print line


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值