473,407 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

How do I save the contents of a text buffer

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

Feb 17 '07 #1
7 5295
On Sat, 17 Feb 2007 15:47:20 -0800, google wrote:
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?

--
Steven.

Feb 18 '07 #2
En Sat, 17 Feb 2007 20:47:20 -0300, <go****@orcon.net.nzescribió:
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...

--
Gabriel Genellina

Feb 18 '07 #3
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
On Feb 18, 1:38 pm, "Gabriel Genellina" <gagsl...@yahoo.com.arwrote:
En Sat, 17 Feb 2007 20:47:20 -0300, <goo...@orcon.net.nzescribió:
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...

--
Gabriel Genellina

Feb 18 '07 #4
On Feb 18, 1:14 pm, Steven D'Aprano
<s...@REMOVE.THIS.cybersource.com.auwrote:
On Sat, 17 Feb 2007 15:47:20 -0800, google wrote:
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!
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?
--
Steven.

Feb 18 '07 #5
On Sat, 17 Feb 2007 17:19:06 -0800, google wrote:
>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
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.

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

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
>I'm guessing... it erased your hard disk. Do I win?

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.
--
Steven.

Feb 18 '07 #6
On Sat, 17 Feb 2007 17:10:50 -0800, google wrote:
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().

--
Steven.

Feb 18 '07 #7
Solved!

# Output file
outfile = open("newbannedsitelist", "w")
outfile.write(textbuffer.get_text(textbuffer.get_s tart_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.

On Feb 18, 7:34 pm, Steven D'Aprano
<s...@REMOVE.THIS.cybersource.com.auwrote:
On Sat, 17 Feb 2007 17:10:50 -0800, google wrote:
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().

--
Steven.

Feb 18 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dave Turner | last post by:
<? $fp = fopen ("binary.dat", "wb"); $buffer = 12345; fwrite ($fp, $buffer); fclose($fp); ?> That creates a file 5 bytes long that contains the text data "12345", but I need it to save that...
7
by: Rolf Hemmerling | last post by:
Hello ! Beginner's question: What ist the easiest way to store and save objects in a file generated by a C++ program, by using the "standard C++ library" and/or "Standard Template Library (...
33
by: Jason Heyes | last post by:
I would like to modify the contents of a file, replacing all occurances of one string with another. I wrote these functions: bool read_file(std::string name, std::string &s); bool...
8
by: Bob Smith | last post by:
I am downloading over http port 80 some contents froma site, but the contents is not properly stored after using ostringstream for temporary storage, and later ostringstream::str() for passing it...
4
by: Jae | last post by:
I'm writing a web application that exports and imports excel files. The application gets a list of users and their info and displays it in a datagrid .The user then selects to save the file as a...
6
by: W.Guerlich | last post by:
I've got a Java servlet that delivers large database resultsets transformed to Excel with the HSSF library. In some cases it takes more than 15 minutes before transformation is done and content can...
2
by: Dave | last post by:
Hello. I need to load an URL and save it to a file in Asp.Net. The function below is creating the file, but isn't putting the data in it. Also the data is binary, so I'm not sure if I need to...
8
by: david.lindsay.green | last post by:
Hello all, I am quite new a web scripting and making web pages in general and I have stumbled across a problem I have as yet been unable to solve. I am trying to take the contents of a textarea box...
1
by: liuliuliu | last post by:
hi -- sorry if this is trivial -- but how do you make a screenshot of a pygame display? i have a surface which is basically the entire visible screen -- how do you write this surface as an image...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.