Skip to content

Instantly share code, notes, and snippets.

@wzpan
Created July 31, 2013 14:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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!
'''
with open('poem.txt', 'w', encoding="utf-8") as f: # 写模式打开
f.write(poem) # 写文件
with open('poem.txt', encoding="utf-8") as f: # 如果没有提供打开模式, 则默认假设为读模式
while True:
line = f.readline()
if not line:
break
print(line, end='')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment