1

I want to split a text file in python, using the following peice of code:

inputfile = open(sys.argv[1]).read()   
for line in inputfile.strip().split("\n"):
    print line

the problem is, that it's read the first 12 lines only!! the file is large more than 10 thousand lines!

What is the possible reason!

Thanks,

userInThisWorld
  • 1,361
  • 4
  • 18
  • 35

2 Answers2

3
with open(sys.argv[1]) as inputfile:
    for line in inputfile:
        print(line)

Use readlines() which will generate list automatically and no need to read by "\n".

Raj Damani
  • 782
  • 1
  • 6
  • 19
0

Try this:

text = r"C:\Users\Desktop\Test\Text.txt"

oFile = open(text, 'r')
line = oFile.readline()[:-1]
while line:
    splitLine = line.split(' ')
    print splitLine
    line = oFile.readline()[:-1]
oFile.close()

I use this style to iterate through huge text files at work

Jake
  • 303
  • 5
  • 19