Skip to content

Instantly share code, notes, and snippets.

@vedal
Last active January 31, 2018 11:31
Show Gist options
  • Save vedal/85f4b54141e85b80b65b2f6e72c34723 to your computer and use it in GitHub Desktop.
Save vedal/85f4b54141e85b80b65b2f6e72c34723 to your computer and use it in GitHub Desktop.
[Correct open,read,write from files] #Python #open #read #write
// 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