0

Something strange happens when I try to write from a ordinary list, containing 648470 string-values, to a text file.

    textFile = open('bins.txt', 'w')

    for item in bigList:
        print item # This prints fine
        textFile.write(item)

    textFile.close()

The text file grows rapidly in file size and is filled with all kind of symbols, not the intended ones... Even if I just write a small span of the content of bigList the text file gets corrupted. If I do the exact same thing with a much smaller list, there's no problem. Is the large size of the list causing this problem? The output of print(bigList[:10]) is

['167', '14444', '118', '22110', '118', '8134', '82', '8949', '7875', '171']
7
  • 2
    Please show the part where you open the file. Commented Feb 7, 2013 at 18:32
  • 1
    @Smajjk Are you opening the file like textFile = open(path, 'w')? And are you sure you are writing text, and not numbers into each line? Commented Feb 7, 2013 at 18:36
  • 1
    I forgot to mention, an extract from the list would be good too - as given by Python if you do print(bigList[:20]), for example. Commented Feb 7, 2013 at 18:44
  • 2
    I notice you're opening this was 'a', not 'w'. What was in the file before? And—not to be patronizing, but I've made this kind of mistake before—are you sure you're looking at the data you appended, rather than the garbage that was already there? In fact, are you sure you appended anything at all (since you don't close the file, it may never flush the buffer to disk)? Commented Feb 7, 2013 at 19:01
  • 2
    What program do you use to view the file? The file consists of one extremely long line; perhaps that triggers a bug in the program. Try another way of viewing the file. Commented Feb 7, 2013 at 20:31

2 Answers 2

1

It works absolutely fine to me.

In your code you are forgetting to close the file, and also, since you open the file in append mode, my guess is that you have some garbage in the file that was there and you forgot to remove.

Also, keep in mind that the write in that way will not separate the numbers in any way.

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

7 Comments

I changed from append mode to write mode and also closes the file. When I write using textFile.write(item) I do still not get the long line of numbers I am expecting but a mess of symbols, looks almost like there are written on top of eachother! But when I use writeFile.write("%s\n" % item) it works?!
What version of python and OS are you using?
xubuntu 12.10 and Python 2.7.3
Are you sure the list only contains string objects?
try this to check: reduce(lambda x,y: x and y, [isinstance(i,str) for i in bigList])
|
0

It is possible that the file is having problems writing because some of the list objects are not strings. Try:

textFile = open('bins.txt', 'w')

for item in bigList:
    print item # This prints fine
    textFile.write(str(item))

textFile.close()

However I cannot see your list so I do not know for certain if this is an actual problem.

1 Comment

Hmm. I'm not sure why this wouldn't write then... It's odd.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.