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

来源:互联网 发布:廊坊管家婆软件 编辑:程序博客网 时间:2024/09/21 08:16
##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 linemy.close()##文件名也就是指针,指向文件的存储的内存##读取文件时需要文件名来寻找file# 文件的三种模式:可读(r),可写(w),追加(a)# 文件的打印,readline()逐行来打印文件的内容##存储器:pickle ,使用它,可以在一个文件中存储任何python对象##之后依旧可以完成无缺的取出来,这被称为持久的存储对象import pickle as pfilename = 'fruit'List = ['apple','orange','banana']f=file(filename,'w')p.dump(List,f)f.close()del List #remove the list##read back from the storagef=file(filename)storedList = p.load(f)# while True:#     line = f.readline()#     if len(line) == 0:#         break#     print line


原创粉丝点击