Chinaunix首页 | 论坛 | 博客
  • 博客访问: 195172
  • 博文数量: 64
  • 博客积分: 2536
  • 博客等级: 少校
  • 技术积分: 610
  • 用 户 组: 普通用户
  • 注册时间: 2009-07-16 16:39
文章分类

全部博文(64)

文章存档

2011年(2)

2010年(1)

2009年(61)

我的朋友

分类: Python/Ruby

2009-09-22 10:21:26

#!/usr/bin/python
# Filename: using_file.py


poem =
'''\
Programming is fun
When the work is done
if you wanna make your work also fun:
        use Python!
'''


f = file('poem.txt', 'w') # open for 'w'riting
f.write(poem) # write text to file
f.close() # close the file

f = file('poem.txt')
# if no mode is specified, 'r'ead mode is assumed by default
while True
:
    line = f.readline()

    if len(line) == 0: # Zero length indicates EOF
        break
    print line,
    # Notice comma to avoid automatic newline added by Python
f.close() # close the file

 

#!/usr/bin/python
# Filename: pickling.py


import cPickle as p
#import pickle as p

shoplistfile = 'shoplist.data'
# the name of the file where we will store the object

shoplist = ['apple', 'mango', 'carrot']

# Write to the file
f = file(shoplistfile, 'w'
)
p.dump(shoplist, f)
# dump the object to a file
f.close()

del shoplist
# remove the shoplist

# Read back from the storage

f = file
(shoplistfile)
storedlist = p.load(f)

print storedlist

阅读(1042) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~