473,569 Members | 2,692 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("mytextfil e", "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("newbanned sitelist", "w")
outfile.write(t extbuffer.get_t ext(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 5302
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.n et.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("mytextfil e", "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.TextBuff er 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(t extbuffer.get_t ext(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.n et.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("mytextfil e", "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.TH IS.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.TextBuff er 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(t extbuffer.get_t ext(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("newbanned sitelist", "w")
outfile.write(t extbuffer.get_t ext(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.

On Feb 18, 7:34 pm, Steven D'Aprano
<s...@REMOVE.TH IS.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.TextBuff er 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(t extbuffer.get_t ext(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
1607
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 value as 32-bits instead (so the file would just be 4 bytes, with the contents being 0x39300000).
7
11471
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 ( STL )" ? So I would like to generate some objects ( of different classes ) with a C++ program and would like to make it permanent / persistent, so...
33
2796
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 write_file(std::string name, const std::string &s); void find_replace(std::string &s, std::string first, std::string second); bool find_replace_file(std::string...
8
1519
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 over to a string. This doesn't work. It misses data and teh result is not the same as when using char buffer, which gives correct download. Could...
4
4513
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 tab delimited file or an excel file. The application then saves the file in the correct format. The flip side is for the user to import/upload the...
6
1940
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 be delivered to the browser. I want to show the "Save As" dialog as early as possible so the user knows he's not lost and forgotten. I...
2
2827
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 fiddle with encoding or whatnot. Dave Private Function GetURLSave(ByVal sURL As String,ByVal sFileName As String) As Boolean Dim oRequest As...
8
3652
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 and save it to a file. This step is not to hard however the contents of the textarea is mostly latex source so it contains just about every special...
1
6785
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 file during specific events in the script execution? image format doesnt matter. thanks! christine
0
7698
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8122
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7673
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7970
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5513
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.