Skip to content

Instantly share code, notes, and snippets.

@NicholasTD07
Created December 18, 2012 03:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NicholasTD07/4324733 to your computer and use it in GitHub Desktop.
Save NicholasTD07/4324733 to your computer and use it in GitHub Desktop.
Read certain lines and split the file.
#!/usr/bin/env python3
# Read lines.
def readLines(filePath, howManyLines=500) :
lines = list()
with open(filePath) as theFile :
number = 0
while True :
for i in range(0, howManyLines) :
line = theFile.readline()
if not line :
return
lines.append(line)
with open('.'.join([filePath, str(number)]), 'w') as splitedFile :
splitedFile.write(''.join(lines))
lines.clear()
number += 1
if __name__ == '__main__' :
from sys import argv
if len(argv) > 2 :
readLines(argv[1], int(argv[2]))
elif len(argv) > 1 :
readLines(argv[1])
else :
print("""\
Please add arguments:
readLines.py file [lines]
file -- the file to split.
lines -- how many line in the splited file.
""")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment