Created
July 31, 2013 14:27
-
-
Save wzpan/6122460 to your computer and use it in GitHub Desktop.
Python - file I/O
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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