472,119 Members | 1,802 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Best way to create temporary file?

Hi all

I need to generate potentially large reports from a database, and I
want to offer the option of print preview before actually printing
(using wxPython). I figure that the best way to achieve this is to
write the report to a temporary file, or more likely to a temporary
directory with a separate file for each page. I can use that for
previewing and for printing, and then delete the file/directory.

There seems to be a few ways to achieve this, and I am not sure of the
best way.

1. I can call tempfile.TemporaryFile() for each file required.

2. I can call tempfile.mkdtemp() to create a temporary directory, and
create each file manually.

3. I can call os.tmpfile() for each file required.

After creating the files, I need to re-read them, either one at a time
in sequence (if printing), or any one at random (if previewing a
particular page). This makes me lean towards method 2.

With this method, I create the files manually, so I can name them
according to their page numbers, which makes it easy to open and close
them as required. The other two methods return open file objects, so
it seems that I cannot close them, as there would be no means of
accessing their contents subsequently. Therefore I would have to
maintain a list of open file objects, use indexing to retrieve a
particular page, and then perform seek(0) before I could read it. A
slight downside of method 2 is that I have to delete the files and the
directory manually when I have finished.

I have two additional questions regarding method 3.

Firstly, I am not sure how it works. The docs say that the file has no
directory entries associated with it, and it will be automatically
deleted when there are no file descriptors for the file. What does
this mean? Does it create a physical file, or is it stored in memory?
If the latter, could I run into memory problems if I create a report
with hundreds of pages?

Secondly, I can create the file using FC3 (Python 2.4), but if I try
on Windows 2000 (Python 2.4) I get the message 'OSError: Permission
denied', even though I am a member of the Administrators group.

Any advice on the best approach for this will be much appreciated.

Frank Millman
Jul 19 '05 #1
4 6592

"Frank Millman" <fr***@chagford.com> ha scritto nel messaggio
news:24**************************@posting.google.c om...
Hi all

I need to generate potentially large reports from a database, and I
want to offer the option of print preview before actually printing
(using wxPython). I figure that the best way to achieve this is to
write the report to a temporary file, or more likely to a temporary
directory with a separate file for each page. I can use that for
previewing and for printing, and then delete the file/directory.

There seems to be a few ways to achieve this, and I am not sure of the
best way.

1. I can call tempfile.TemporaryFile() for each file required.

2. I can call tempfile.mkdtemp() to create a temporary directory, and
create each file manually.

3. I can call os.tmpfile() for each file required.

After creating the files, I need to re-read them, either one at a time
in sequence (if printing), or any one at random (if previewing a
particular page). This makes me lean towards method 2.

With this method, I create the files manually, so I can name them
according to their page numbers, which makes it easy to open and close
them as required. The other two methods return open file objects, so
it seems that I cannot close them, as there would be no means of
accessing their contents subsequently. Therefore I would have to
maintain a list of open file objects, use indexing to retrieve a
particular page, and then perform seek(0) before I could read it. A
slight downside of method 2 is that I have to delete the files and the
directory manually when I have finished.

I have two additional questions regarding method 3.

Firstly, I am not sure how it works. The docs say that the file has no
directory entries associated with it, and it will be automatically
deleted when there are no file descriptors for the file. What does
this mean? Does it create a physical file, or is it stored in memory?
If the latter, could I run into memory problems if I create a report
with hundreds of pages?

Secondly, I can create the file using FC3 (Python 2.4), but if I try
on Windows 2000 (Python 2.4) I get the message 'OSError: Permission
denied', even though I am a member of the Administrators group.

Any advice on the best approach for this will be much appreciated.

Frank Millman

Why create an intermediate file to preview with wxPython? I would just
preview the data with wx and keep it inside your application. If you want to
print it just print... avoid temp file creation if it's not necessary... :)

F.P.
Jul 19 '05 #2
Frank Millman wrote:
Hi all

I need to generate potentially large reports from a database, and I
want to offer the option of print preview before actually printing
(using wxPython). I figure that the best way to achieve this is to
write the report to a temporary file, or more likely to a temporary
directory with a separate file for each page. I can use that for
previewing and for printing, and then delete the file/directory.

Fabio Pliger wrote: Why create an intermediate file to preview with wxPython? I would just
preview the data with wx and keep it inside your application. If you want to
print it just print... avoid temp file creation if it's not necessary... :)

F.P.


It depends on what you mean, Fabio.

If a report contains sub-totals, page breaks, etc, it is not possible
to preview a particular page at random without generating the entire
report first.

It is certainly possible to store the entire report in memory, using a
two-dimensional list (page/line), but if a report runs into hundreds of
pages, I am concerned at the amount of memory this would require.
Perhaps I am being old-fashioned - with todays memory of at least 64k,
it would probably fit without a problem - but I would prefer to write
the pages away and read them back when needed.

Frank
Jul 19 '05 #3
Frank Millman wrote:
It is certainly possible to store the entire report in memory, using a
two-dimensional list (page/line), but if a report runs into hundreds of
pages, I am concerned at the amount of memory this would require.
Perhaps I am being old-fashioned - with todays memory of at least 64k,
it would probably fit without a problem - but I would prefer to write
the pages away and read them back when needed.


Modern operating systems have a technique for dealing with this. It is
called virtual memory. You are only using up more memory, CPU, and disk
speed by using temporary files unnecessarily.
--
Michael Hoffman
Jul 19 '05 #4
Ok, thanks for the advice.

Frank

Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by James Proctor | last post: by
14 posts views Thread by Howard | last post: by
4 posts views Thread by Asaf | last post: by
1 post views Thread by James Proctor | last post: by
10 posts views Thread by jojobar | last post: by
4 posts views Thread by lwoods | last post: by
reply views Thread by leo001 | last post: by

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.