Super Newbie Question

J

joeyjwc

I have recently started to use Python to develop a new version of my
website.

I'm working on a sort of "web engine" that takes the contents of a
standardized file format I have designed and then outputs it using
templates created by me.

However, I'm only at the very beginning (i.e. opening and reading
files) because I'm a newbie to Python. I want to spend a lot of time
learning Python.

I have searched with about 15 million different key phrases on Google
and Google Groups but haven't come up with an answer to my following
question:

I want the "engine" to read the file, write its contents to another
temporary file (for now it just writes the contents; later it will
format it before writing the contents) and then deletes the *contents*
of the temporary file after printing out the result, but not the
temporary file itself. I want it to do this because that temporary
file will be reused to write to for other pages as well.

In short, how might I go about deleting just the contents of a file?
I tried several methods with my limited knowledge but had no luck.

Thank you very much for answering a lowly newbie's question. I really
appreciate it.

P.S. If there's a better that you think to perform what I am saying,
I'd like to be enlightened. But I'd also like my question answered
too, in case I ever need to do something similar again.
 
R

Roman Neuhauser

# (e-mail address removed) / 2005-04-04 16:39:27 -0700:
In short, how might I go about deleting just the contents of a file?
I tried several methods with my limited knowledge but had no luck.

fd = open("your-file")
fd.truncate()
fd.close()

or open("your-file", "w").close()
 
B

Brian van den Broek

(e-mail address removed) said unto the world upon 2005-04-04 19:39:

I want the "engine" to read the file, write its contents to another
temporary file (for now it just writes the contents; later it will
format it before writing the contents) and then deletes the *contents*
of the temporary file after printing out the result, but not the
temporary file itself. I want it to do this because that temporary
file will be reused to write to for other pages as well.

In short, how might I go about deleting just the contents of a file?
I tried several methods with my limited knowledge but had no luck.

Thank you very much for answering a lowly newbie's question. I really
appreciate it.

P.S. If there's a better that you think to perform what I am saying,
I'd like to be enlightened. But I'd also like my question answered
too, in case I ever need to do something similar again.

Hi,

if I understand your question, I think the answer to how to delete
will tell you that you don't need to worry about it.

That will write an empty string to the file, erasing all its contents,
while preserving the file in your file system (as one of 0 bytes).

So, I think this shows that your worry (at least as I understood it is
misplaced. Unless you have a free space on the disk issue at hand, you
don't need to bother with a delete step -- just open the file in write
mode when you want to write to it again, and that will over-write any
extant contents.

HTH,

Brian vdB
 
G

Greg Ewing

In short, how might I go about deleting just the contents of a file?

File objects have a truncate() method which chops the file
back to zero length (or a length that you specify).

But why not just delete the file? You can always
create another one with the same name later if
you want.
 
J

Joey C.

To reply to many of your messages (I'm using Google right now due to
lack of a better newsreader at the moment), the issue with the
temporary file is that when I write something new to it, if the old
contents of the file was larger, not all of it will be overwritten.
So, the truncate() method will work very well. Thank you Mr.
Neuhauser, Mr. Ewing, and all others.
 
P

Peter Hansen

Joey said:
To reply to many of your messages (I'm using Google right now due to
lack of a better newsreader at the moment), the issue with the
temporary file is that when I write something new to it, if the old
contents of the file was larger, not all of it will be overwritten.

That's not likely true, unless you're doing something
unusual here. Are you somehow terminating the process
before it closes the file (and not using flush())?
Or are you opening the file in some kind of "update"
mode (with a "+") instead of just using "w"? Or
something else?

Or are you just assuming that what you describe will
happen, but haven't actually tried it?

-Peter
 
G

Greg Ewing

Joey said:
the issue with the
temporary file is that when I write something new to it, if the old
contents of the file was larger, not all of it will be overwritten.

Not if you open it with

f = open(filename, "w")

which will delete any previous contents the file
may have had.
 

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

Similar Threads


Members online

Forum statistics

Threads
474,234
Messages
2,571,178
Members
47,808
Latest member
sunnysingh55

Latest Threads

Top