python写文件

来源:互联网 发布:长沙java培训 编辑:程序博客网 时间:2024/05/17 23:24

#!/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 'w'不追加 'a+'是追加写(貌似) /r/n换行
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

原创粉丝点击