(solved) help with program calling to and writing to a txt document | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

(solved) help with program calling to and writing to a txt document

I have used this code before and it worked in a different program. the program for reading the code works just fine but this code wont right to the text document. link to code https://code.sololearn.com/cLJGMS6koalW I only put the code I thought was important to this issue any help would be great link to read comment code https://code.sololearn.com/c2l9N4V5GgDy

24th Aug 2021, 9:30 PM
David Aseltine
David Aseltine - avatar
6 Answers
+ 3
I see that you figured out a solution, so all is well - perhaps. I was pondering, if it is a text file, then why does the file-writing code use binary file mode "b"? And shouldn't it be "wb" (if not "w")? And if binary is really what was intended, then doesn't the string have to be converted to a bytes object? For example, comment = open("comment.txt","wb") comment1 = input("leave comment here : ") comment.write(bytes("~~~"+comment1+"~~~", encoding="utf8"))
24th Aug 2021, 10:37 PM
Brian
Brian - avatar
+ 3
Delicate Cat after doing a little research and a quick programming test, I concur 'del comment' would close the file. The IOBase class provides a default __del__ method that calls close().
25th Aug 2021, 1:04 PM
Brian
Brian - avatar
+ 2
Brian Oh, that sounds logical. Thank you for the answer.
25th Aug 2021, 2:30 PM
Calvin Thomas
Calvin Thomas - avatar
+ 1
the issue was that I was trying to call to a text file that wasn't in the right location I created a text file in the write folder and changed the text files name and now the code works just fine
24th Aug 2021, 9:52 PM
David Aseltine
David Aseltine - avatar
+ 1
Brian In David Aseltine's code, doesn't 'del comment' also close the file?
25th Aug 2021, 4:48 AM
Calvin Thomas
Calvin Thomas - avatar
0
Brian the code should actually have "a" not "b" I was changing little things to attempt to fix it and ended up changing it back to "a" after I figured out the real issue
25th Aug 2021, 2:34 PM
David Aseltine
David Aseltine - avatar