0

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?

5
  • Erm, if rereading doesn't solve the bigger problem then I'm not sure that I want to know what the bigger problem is... Commented Mar 6, 2015 at 2:52
  • Your question is not clear. What are you trying to achieve? Commented Mar 6, 2015 at 2:54
  • I am trying to make a COPY of the variable 'text'. So I basically want 'text2' to refer to something (is it really called text file?) that is the same as 'text' but I don't want them to be CONNECTED. :) Commented Mar 6, 2015 at 2:57
  • Do you want text to be a file object or a list of strings? Commented Mar 6, 2015 at 2:59
  • I want it to be the same as what 'text' is, the thing you get when you write {open("textfile.txt","r"]} Commented Mar 6, 2015 at 3:03

3 Answers 3

7

If you're trying to avoid re-opening the file, but want to read it twice, you can use 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).

Sign up to request clarification or add additional context in comments.

Comments

2

Alternatively, if you really do want to wind up with two files for some reason, it's best to use shutil.copy to copy it:

import shutil

path = "wordsv.txt"
path2= "wordsv2.txt"
shutil.copy(path, path2)
with open(path, encoding='utf-8') as text, open(path2, encoding='utf=8') as text2:
    # Do something with text, text2 here

Comments

1

Your line text2 = copy.copy(text) does not work because text is a file object only. To "copy" the text in the file, do as follows:

text2 = text.read()

Please note that you don't copy the actual text (content), as strings are immutable.

2 Comments

The problem with that is that I lose text, i.e. it becomes "read". I want to keep them both unread.
@user3128156 than maybe have a look at samGbos answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.