Search code examples
pythonpython-3.xfunctionfile-handlingwith-statement

How to write a specific portion in text file using file handling in python?


I am working on a project and want to print multiple lines in a text file. This is the method I used for this purpose.

def story_part(file_path,initial_index,final_index):
    line_number = list(range((initial_index -1) ,final_index ))
    mylines = []

    with open(file_path) as f:
        for i , line in enumerate(f):
            if i in line_number:

                mylines.append(line.rstrip())
            elif i > final_index:
                break
    for content in mylines:
        print(content)

can you type more efficient code?

I was trying to print in a specified portion from a text file. I have searched through several websites and didn't find something helpful.

and after scratching for some time, I come up with this function. Is this the correct way, or you can help it to improve?


Solution

  • One optimization you could probably do in your code (wihtout having to change too much) is to make your line_number to be a set so that the index lookup becomes constant.

    i in [1 2 3] # O(n) 
    i in {1 2 3} # O(1)
    

    The current answers are perfectly fine. But, read(), readlines(), splitlines() loads the entire data into the memory. Which might not be very scalable when the file size is large.

    You could probably make use of itertools to read the file as an iterator.

    from itertools import islice
    def story_part(file_path,initial_index,final_index):
    
        with open(file_path, "r") as f:
            # islice the file iterator from start index to end index. (You can also pass a step to islice)
            # map your rstrip to each line that is read.
            # list call is not necessary here if you are looping over the data within the scope of `with`. To use the data elsewhere, you will have to realize it with the list call.
            mylines = list(map(str.rstrip, islice(f, initial_index-1, final_index)))
        for content in mylines:
            print(content)
    

    This way file is read only till final_index and only the required lines are ever loaded into the memory. (Note that all lines of the files prior to the initial_index will be read, but ignored.)