2

I am new to python and have until now written only a few programs to help with my job (I'm a sysadmin). I am writing this script now which will write the output of a MySQL query to a file. While in-between looping, I want to check for an extra condition and if the condition does not match, I want to close the file that I am writing to without saving what it has already written to the file. Like 'exit without saving'. I wrote this simple code to see if not closing the file with a close() will exit without saving, but it is creating the file with the content after I run and exit this code. So, is there a legal way in Python to exit a file without saving?

#/usr/bin/python
fo=open('tempfile.txt','a')
fo.write('This content\n')

P.S:- Python version is 2.4.3 (sorry, cannot upgrade)

Sreeraj
  • 137
  • 7

4 Answers4

5

There is no such concept in programming.

For the vast majority of the programming languages out there, the write command will attempt to put data directly in the file. This may or may not occur instantly for various reasons so many languages also introduce the concept of flush which will guarantee that your data is written to the file

What you want to do instead is to write all your data to a huge buffer (a string) then conditionally write or skip writing to the file.

Eric
  • 19,525
  • 19
  • 84
  • 147
  • 2
    The concept of temporary files does exist, and they're often a better idea than giant memory buffers. – tzaman Dec 09 '14 at 02:58
  • The file I am writing to, is an HTML code(pretty big). I thought I will use this as a last resort. – Sreeraj Dec 09 '14 at 03:05
  • You are right about temp files, but this involves file manipulation such as renaming and maybe moving which seemed to go beyond the scope of the current question level – Eric Dec 09 '14 at 03:07
  • http://stackoverflow.com/questions/1739913/what-is-the-max-length-of-a-python-string A few mb should be handled by python without too much problems. It is common practice to fetch a webpage into a string and parse it from there – Eric Dec 09 '14 at 03:10
4

Use the tempfile module to create your temporary, then if you need to save it you can do so explicitly using shutil.copyfileobj.

See Quick way to save a python TempFile?

Note that this is only if you absolutely need a temporary file (large amounts of data, etc.); if your contents are small then just using a stringbuffer and only writing it if you need to is a better approach.

Community
  • 1
  • 1
tzaman
  • 46,925
  • 11
  • 90
  • 115
1

Check for the condition before opening the file:

#/usr/bin/python

if condition():

    fo=open('tempfile.txt','a')
    fo.write('This content\n')
Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331
0

The safest way to do this is not to write to, or even open, the file until you know you want to save it. You could, for example, save what you might eventually write to a string, or list of same, so you can write them once you've decided to.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101