How to copy text file in python
Question: Here's what I want to do:

    
    
    import copy
    def printtext(swefile):
        for row in swefile:
            print(row)
    text = open("wordsv.txt","r",encoding="utf-8")
    text2 = copy.copy(text)
    printtext(text)
    print(text2.readlines())
    

But it is not possible, TypeError: cannot serialize '_io.TextIOWrapper'
object. So I wonder if there is a good method for "cloning" the text variable,
so that I could print all the lines again. I know that I could just read the
file again, but that answer does not solve the bigger problem I have so any
suggestion on how to get this done is helpful.

* * *

Here's the larger context as I haven't been able to solve my problem with your
suggestions:

    
    
    with open(textfilename, "r", encoding = "utf-8") as swefile:
        for row in swefile:
            word = row.strip()
            tempfile = copy.copy(swefile)
            l = getFurthest(word,tempfile)
    

The thing that I want to happen here is that i want to send the portion of
`swefile` that has not been read yet (i.e. iterated over yet by the for loop)
to `getFurthest()`! And I can't send `swefile` because that will make the
whole thing read so the iterations in the for loop will stop, right? So how
can I send only the portion of the textfile that has been read to
`getFurthest()` while still being able to iterate over the rest of it after?


Answer: If you're trying to avoid re-opening the file, but want to read it twice, you
can use [`seek()`](https://docs.python.org/3/library/io.html#io.IOBase.seek):

    
    
    import copy
    def printtext(swefile):
        for row in swefile:
            print(row)
    text = open("wordsv.txt","r",encoding='utf-8')
    printtext(text)
    text.seek(0)
    printtext(text)
    

If you just care about the text, you can do this:

    
    
    import copy
    def printtext(swefile):
        for row in swefile:
            print(row)
    text = open("wordsv.txt","r",encoding='utf-8').readlines()
    text2 = copy.copy(text)
    printtext(text)
    printtext(text2)
    

Here `text` is a list of the lines in `wordsv.txt`, and you then copy the list
into text2 (i.e. changing `text` won't change `text2`).

