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)

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

4 Comments

The concept of temporary files does exist, and they're often a better idea than giant memory buffers.
The file I am writing to, is an HTML code(pretty big). I thought I will use this as a last resort.
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
stackoverflow.com/questions/1739913/… 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
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.

Comments

1

Check for the condition before opening the file:

#/usr/bin/python

if condition():

    fo=open('tempfile.txt','a')
    fo.write('This content\n')

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.