Skip to content

Instantly share code, notes, and snippets.

@wzpan
Created July 31, 2013 14:27
Show Gist options
  • Save wzpan/6122460 to your computer and use it in GitHub Desktop.
Save wzpan/6122460 to your computer and use it in GitHub Desktop.
Python - file I/O
#!/usr/bin/python
# Filename: file_io.py
poem = '''\
Programming is fun
When the work is done
if you wanna make your work also fun:
use Python!
'''
f = open('poem.txt', 'w', encoding="utf-8") # 写模式打开
f.write(poem) # 写文件
f.close() # 关闭文件
f = open('poem.txt', encoding="utf-8") # 如果没有提供打开模式, 则默认假设为读模式
while True:
line = f.readline()
if not line:
break
print(line, end='')
f.close() # 别忘了关闭文件
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment