Ask Question
29 August, 23:34

Write a program that Read first n lines of file

+1
Answers (1)
  1. 29 August, 23:58
    0
    Python 2:

    with open ("datafile") as myfile:

    head = [next (myfile) for x in xrange (N) ]

    print head

    Python 3:

    with open ("datafile") as myfile:

    head = [next (myfile) for x in range (N) ]

    print (head)

    Both Python 2 & 3:

    from itertools import islice

    with open ("datafile") as myfile:

    head = list (islice (myfile, N))

    print head
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a program that Read first n lines of file ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers