How do I save the contents of a text buffer

G

google

Hi,

I'm using Python with pygtk and have this problem - I have read the
contents of a file into the text buffer with this code,

infile = open("mytextfile", "r")
if infile:
string = infile.read()
infile.close()
textbuffer.set_text(string)

As a test, I tried to write the buffer back to a file with this code
but did not work,

outfile = open("newbannedsitelist", "w")
outfile.write(textbuffer.get_text(0, 1000,
include_hidden_chars=True))
outfile.close()

What I want to know is how do I write the contents of the text buffer
back to a file?

Thanks
 
S

Steven D'Aprano

As a test, I tried to write the buffer back to a file with this code
but did not work,

Oooh, guessing games! I love guessing games!

Let me see... did it reboot your PC?

Did Python crash?

Did you get an exception? Maybe something about not being able to open
the file for reading? Or perhaps disk full?

Did you get something unexpected in the file? Maybe an empty file?

I'm guessing... it erased your hard disk. Do I win?
 
G

Gabriel Genellina

I'm using Python with pygtk and have this problem - I have read the
contents of a file into the text buffer with this code,

infile = open("mytextfile", "r")
if infile:
string = infile.read()
infile.close()
textbuffer.set_text(string)

Note that "if infile" does nothing: either the open succeeds and you get a
file object which is *always* True, or the open fails and raises an
exception and the code below never executes.
As a test, I tried to write the buffer back to a file with this code
but did not work,

"did not work" means...
 
G

google

I just included file opening code just to show how i read the file
into the text buffer - I have no issues with this as such. Problem is
only with the writing of the text buffer back to a file. When I try to
write the buffer to a file it gave the following,

<gtk.TextBuffer object (GtkTextBuffer) at 0xb7cff284>
Traceback (most recent call last):
File "./configbox.py", line 78, in ?
TextViewExample()
File "./configbox.py", line 53, in __init__
outfile.write(textbuffer.get_text(0,1000,
include_hidden_chars=True))
TypeError: start should be a GtkTextIter

How can I use outfile.write() to wite the contents of the text buffer
correctly?

Thanks
 
G

google

Oooh, guessing games! I love guessing games!
Good

Let me see... did it reboot your PC?
No

Did Python crash?

No - it runs on nix
Did you get an exception? Maybe something about not being able to open
the file for reading? Or perhaps disk full?

File read ok for input, its the file write thats the problem
Did you get something unexpected in the file? Maybe an empty file?

Yes, a rabbit popped out of the floppy slot - amazing!
I'm guessing... it erased your hard disk. Do I win?

Sorry, you lose....are you a Windows user?
 
S

Steven D'Aprano

File read ok for input, its the file write thats the problem

Well, duh. I know that -- that's what your first email said.

Don't tell us there's a problem. Tell us what the problem is. Duh.

Yes, a rabbit popped out of the floppy slot - amazing!

You know how your teachers said "there are no stupid questions, only
stupid answers?" They lied.

Perhaps you should go away and read this before asking any further
questions.

http://catb.org/esr/faqs/smart-questions.html


Sorry, you lose....are you a Windows user?


Sorry, you lose.

I really don't know why you're casting aspersions on the intelligence of
Windows users, when you're the one posting a stupid question. Do I really
need to spell it out?

We can't tell you what the solution to the problem is if you won't tell us
what the problem is. If you get an exception, post the exception. If
something unexpected happens, tell us what it was. We're not mind-readers.
 
S

Steven D'Aprano

I just included file opening code just to show how i read the file
into the text buffer - I have no issues with this as such. Problem is
only with the writing of the text buffer back to a file. When I try to
write the buffer to a file it gave the following,

<gtk.TextBuffer object (GtkTextBuffer) at 0xb7cff284>
Traceback (most recent call last):
File "./configbox.py", line 78, in ?
TextViewExample()
File "./configbox.py", line 53, in __init__
outfile.write(textbuffer.get_text(0,1000,
include_hidden_chars=True))
TypeError: start should be a GtkTextIter


Ah, well there's your problem. start should be a GtkTextIter, just like
the exception says.

Question for you: in the line of code in the traceback, which function
takes an argument called "start"?

How can I use outfile.write() to wite the contents of the text buffer
correctly?

Your problem isn't with outfile.write().
 
G

google

Solved!

# Output file
outfile = open("newbannedsitelist", "w")
outfile.write(textbuffer.get_text(textbuffer.get_start_iter(),
textbuffer.get_end_iter(), include_hidden_chars=True))
outfile.close()

I'm new to Python and GTK and may not be asking the right type of
questions initially. I programmed with C many many years ago and my
programming instincts are very rusty so still feeling my way again
with this stuff. Apologies for the sarcastic reply earlier but i felt
your initial reply to be sarcastic. I also did research this problem
but seemed to be getting nowhere - thats why I asked for the groups
help.

Anyway, thanks to all that replied via emailed and in this group.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top