2

I'm really frustrated with this strange behaviour of Python all of a sudden. I've been writing to files all sorts of data but since today morning it just doesn't seem to work. I've referred to all these before posting:

Have tried all following commands but it just doesn't write anything to the delete.txt file. What's happening?

fl=open('delete.txt','w')
fl.write(msg) <--Doesnt work,tried this also
fl.write('%s' %msg) <--Doesnt work,tried this also
fl.write("at least write this") <-- Doesnt work,tried this also
print (msg) <- WORKS

Code:

for i in hd_com.comment_message[1:500]:
fl=open('delete.txt','wb')

try:
    if len(i)>40:
        mes=processComUni(i)
        proc=nltk.tokenize.word_tokenize(mes)
        #print proc
        pos=nltk.pos_tag(proc)
        for i in pos:
            if ((i[1]=="NN") or (i[1]=="NNP") or (i[1]=="NNS")) and len(i[0])>2:
                #print i[0],i[1]
                for j in home_depo_inv:
                    if i[0] in j.split() and (i[0]!='depot' and i[0]!='home' and i[0]!='store' and i[0]!='por' and i[0]!='get' and i[0]!='house' and i[0]!='find' and i[0]!='part' and i[0]!='son' and i[0]!='put' and i[0]!='lot' and i[0]!='christmas' and i[0]!='post'):
                        a=re.findall(i[0],j)
                        fl.write(str(i))<--Doesnt work,tried this also
                        fl.write(str(mes))<--Doesnt work,tried this also
                        fl.write("\n")<--Doesnt work,tried this also
                        fl.write("hello")<--Doesnt work,tried this also
                        fl.flush()
                        break
except:
    continue
fl.close()

More code:

type(mes) = str
mes="omg would love front yard"
15
  • Do you get an error? How do these not work? Commented Dec 9, 2014 at 6:21
  • no errors on console. Why is it that prints working fine, but its not writing the same to a file ? Commented Dec 9, 2014 at 6:22
  • whats the type of msg ? try type(msg) Commented Dec 9, 2014 at 6:24
  • Try print(msg,file=fl) Commented Dec 9, 2014 at 6:26
  • 1
    Change the except: to except Exception as exp: and then print the except out (rather than ignoring it). Commented Dec 9, 2014 at 8:02

2 Answers 2

3

Your snippet's indentation is totally messed up, but anyway: your code starts with:

for i in hd_com.comment_message[1:500]:
    fl=open('delete.txt','wb')

which means you reopen the file for writing on each iteration, erasing whatever might have been written by the previous iteration.

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

Comments

1

You need to flush the output stream explicitly when writing to a file handle like that.

f = open("test.txt", "w")
f.write("this is a test\n")
# no text in the output file at this point
f.flush()
# buffers are flushed to the file
f.write("this, too, is a test\n")
# again, line doesn't show in the file
f.close()
# closing the file flushes the buffers, text appears in file

From the documentation of file.write:

Note that due to buffering, flush() or close() may be needed before the file on disk reflects the data written.

2 Comments

Doesnt work either, have updated my code with your suggestions in question, still no luck
its writing only the last message (mes) to the file. Not writing the previous lines. I guess they are getting overwritten or something. Whats even happening here.

Your Answer

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