Search code examples
pythonpython-2.7splitstrip

Split text file into lines, Python


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,


Solution

  • 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".