Created
December 18, 2012 03:23
-
-
Save NicholasTD07/4324733 to your computer and use it in GitHub Desktop.
Read certain lines and split the file.
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/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