clear content of 'printed'

  • Thread starter Christian Otteneuer
  • Start date
C

Christian Otteneuer

Hello Newsgroup,

I have the following problem:
I want to write a python script, that writes text into files. The text is
being "collected" in a for-loop, the code looks like this:

for i in range(len(myList)):
id = myList
print "some text"
container.writeToFile(id, printed)

Where writeToFile() creates a new file with the id 'id' and fills it with
'printed'. The problem is, that I have to delete the
content of 'printed' before entering the for-loop again. Else, I'll find
the content of the past for-loops in my file. (The file created in the
second loop would look like this:)

some text
some text

Is there a possibility to do clear the content of 'printed' in python?

Thanks alot!!
 
P

Peter Otten

Christian said:
Is there a possibility to do clear the content of 'printed' in python?

What is 'printed'? A string, a list, a dictionary, a StringIO instance... -
as long as we do not know what it is we cannot say how/whether it can be
cleared. It is common practice in Python to just forget about an old object
and create a new one, though:

for path in aList:
printed = Printed()
printed.fillWith("some text")
container.writeToFile(path, printed)

Or was the

print "some text"

statement for real and you are assigning to sys.stdout (generally a bad
idea)?

Please provide the complete source code or at least some more context.

Peter
 
J

Jeff Epler

What is "printed" (and why do you think that 'print "some text"' will
modify it)? What is "container", that it has a "writeToFile" method?
You must be using some non-standard module you didn't mention.

If I understand what you're asking for, I might write something like
for i, id in enumerate(myList):
f = open(id, "w")
print >>f, "some text"
(Enumerate is a new built-in function in Python 2.3. The "print >>"
syntax has been available since 2.1 or so)

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)

iD8DBQFBKKlLJd01MZaTXX0RAk0ZAJ9gQP9UwHuYN1ibijw15KlMoHN27wCffKEF
ecjN48PxVLS15Jq+wDG7JHw=
=0t/p
-----END PGP SIGNATURE-----
 
E

Evan Simpson

Christian said:
I have the following problem:
I want to write a python script, that writes text into files. The text is
being "collected" in a for-loop, the code looks like this:

for i in range(len(myList)):
id = myList
print "some text"
container.writeToFile(id, printed) [...]
Is there a possibility to do clear the content of 'printed' in python?


This question properly belongs in one of the Zope lists, since the
regular Python folks have no idea what you're talking about. Thank
goodness I spotted you, or wackiness would ensue ;-)

"printed" is a magic variable implemented by the Script machinery, and
is read-only. You need to drop print and use plain strings:

for i in range(len(myList)):
id = myList
txt = "some text"
container.writeToFile(id, txt)

Cheers,

Evan @ 4-am
 
C

Christian Otteneuer

Evan Simpson said:
"printed" is a magic variable implemented by the Script machinery, and is
read-only. You need to drop print and use plain strings:

for i in range(len(myList)):
id = myList
txt = "some text"
container.writeToFile(id, txt)



Thanks! That's the way I'll do it...
 

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
474,204
Messages
2,571,063
Members
47,670
Latest member
micheljon

Latest Threads

Top