Last active
January 31, 2018 11:31
-
-
Save vedal/85f4b54141e85b80b65b2f6e72c34723 to your computer and use it in GitHub Desktop.
[Correct open,read,write from files] #Python #open #read #write
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
// vim: syntax=python | |
# In Python 2.6+, you could use io.open() that is default (builtin open()) on Python 3: | |
import io | |
with io.open("filename.txt", 'r') as file: | |
# count lines of file | |
num_lines = sum(1 for line in file) | |
# get N lines | |
N = 3 | |
for i in range(N): | |
print file.next() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment