How to quit without saving the file in Python


Light of the city

I'm new to python and have only written a few programs so far to help me with my work (I'm a sysadmin). I am now writing this script which will write the output of a MySQL query to a file. Between loops, I want to check for an additional condition, and if the condition doesn't match, I want to close the file it's writing to without saving the file it's already written to. Like "exit without saving". I wrote this simple code to see if closing the file without using close() will exit without saving, but after running and exiting this code it will create the file with the content. 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')

PS:- Python version is 2.4.3 (sorry, can't upgrade)

Aaron Hall

Check the condition before opening the file:

#/usr/bin/python

if condition():

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

Related


How to quit without saving the file in Python

Light of the city I'm new to python and have only written a few programs so far to help me with my work (I'm a sysadmin). I am now writing this script which will write the output of a MySQL query to a file. Between loops, I want to check for an additional cond

How do I quit without saving the file in Python

Light of the city I'm new to python and have only written a few programs so far to help me with my work (I'm a sysadmin). I am now writing this script which will write the output of a MySQL query to a file. Between loops, I want to check for an additional cond

How do I quit without saving the file in Python

Light of the city I'm new to python and have only written a few programs so far to help me with my work (I'm a sysadmin). I am now writing this script which will write the output of a MySQL query to a file. Between loops, I want to check for an additional cond

How do I quit without saving the file in Python

Light of the city I'm new to python and have only written a few programs so far to help me with my work (I'm a sysadmin). I am now writing this script which will write the output of a MySQL query to a file. Between loops, I want to check for an additional cond

How to generate a file without saving it to disk in python?

Sasha Kalaba I am using Python 2.7 and Django 1.7. I have a method in my admin interface that generates some kind of csv file. def generate_csv(args): ... #some code that generates a dictionary to be written as csv .... # this creates a direct

How to generate a file without saving it to disk in python?

Sasha Kalaba I am using Python 2.7 and Django 1.7. I have a method in my admin interface that generates some kind of csv file. def generate_csv(args): ... #some code that generates a dictionary to be written as csv .... # this creates a direct

How to generate a file without saving it to disk in python?

Sasha Kalaba I am using Python 2.7 and Django 1.7. I have a method in my admin interface that generates some kind of csv file. def generate_csv(args): ... #some code that generates a dictionary to be written as csv .... # this creates a direct

How to quit Excel with win32com without saving?

List When I do xl.Quit(), Excel always pops up and prompts you if you want to save. How can I tell I don't want to save? xl = Dispatch('Excel.Application') xl.Workbooks.Open('New Workbook.xlsx') # do some stuff xl.Quit() Xukrao from win32com.client import Di

How to quit Excel with win32com without saving?

List When I do xl.Quit(), Excel always pops up and prompts you if you want to save. How can I tell I don't want to save? xl = Dispatch('Excel.Application') xl.Workbooks.Open('New Workbook.xlsx') # do some stuff xl.Quit() Xukrao from win32com.client import Di

How to quit Excel with win32com without saving?

List When I do xl.Quit(), Excel always pops up and prompts you if you want to save. How can I tell I don't want to save? xl = Dispatch('Excel.Application') xl.Workbooks.Open('New Workbook.xlsx') # do some stuff xl.Quit() Xukrao from win32com.client import Di

How to quit Excel with win32com without saving?

List When I do xl.Quit(), Excel always pops up and prompts you if you want to save. How can I tell I don't want to save? xl = Dispatch('Excel.Application') xl.Workbooks.Open('New Workbook.xlsx') # do some stuff xl.Quit() Xukrao from win32com.client import Di

How to quit Excel with win32com without saving?

List When I do xl.Quit(), Excel always pops up and prompts you if you want to save. How can I tell I don't want to save? xl = Dispatch('Excel.Application') xl.Workbooks.Open('New Workbook.xlsx') # do some stuff xl.Quit() Xukrao from win32com.client import Di

(Python 3) How to pass binary file as text without saving first

Rolf Bly Or, maybe a better title: How to avoid unnecessary extra carriage returns when passing a binary file to a text mode write clause. Python 3.6, Windows. The input file needs to be binary search/replace first and then regular expression search/replace. I

(Python 3) How to pass binary file as text without saving first

Rolf Bly Or, maybe a better title: How to avoid unnecessary extra carriage returns when passing a binary file to a text mode write clause. Python 3.6, Windows. The input file needs to be binary search/replace first and then regular expression search/replace. I

(Python 3) How to pass binary file as text without saving first

Rolf Bly Or, maybe a better title: How to avoid unnecessary extra carriage returns when passing a binary file to a text mode write clause. Python 3.6, Windows. The input file needs to be binary search/replace first and then regular expression search/replace. I

(Python 3) How to pass binary file as text without saving first

Rolf Bly Or, maybe a better title: How to avoid unnecessary extra carriage returns when passing a binary file to a text mode write clause. Python 3.6, Windows. The input file needs to be binary search/replace first and then regular expression search/replace. I

How to load Raw .NEF file into QImage without saving it in Python

Ziara start things. I am new to Python image workflow. But I'm pretty good at python in general. But I can't seem to find a good solution to display RAW .NEF files with Nikon cameras. What I want to do is open the RAW file, convert it to a QImage or QPixmap, a

(Python 3) How to pass binary file as text without saving first

Rolf Bly Or, maybe a better title: How to avoid unnecessary extra carriage returns when passing a binary file to a text mode write clause. Python 3.6, Windows. The input file needs to be binary search/replace first and then regular expression search/replace. I

(Python 3) How to pass binary file as text without saving first

Rolf Bly Or, maybe a better title: How to avoid unnecessary extra carriage returns when passing a binary file to a text mode write clause. Python 3.6, Windows. The input file needs to be binary search/replace first and then regular expression search/replace. I

How to load Raw .NEF file into QImage without saving it in Python

Ziara start things. I am new to Python image workflow. But I'm pretty good at python in general. But I can't seem to find a good solution to display RAW .NEF files with Nikon cameras. What I want to do is open the RAW file, convert it to a QImage or QPixmap, a

How to load Raw .NEF file into QImage without saving it in Python

Ziara start things. I am new to Python image workflow. But I'm pretty good at python in general. But I can't seem to find a good solution to display RAW .NEF files with Nikon cameras. What I want to do is open the RAW file, convert it to a QImage or QPixmap, a

How to convert python to exe without saving strings in exe file?

Yov Sharev I want to use Nuitka/Pyinstaller to convert my python project to Exe file, but unfortunately all the strings I write in python file can be easily found in Exe file by opening the Exe file with Notepad. How can I convert my project to Exe and complet

Vim map: q to quit without saving

At last I want Vim to exit without saving when exiting :q. It annoys me when Vim prompts me to save when I quit the document when I try to :qchange it . Whenever I want to keep changes, I save them. add! The :q!position sits awkwardly on my UK keyboard. How ca

Vim map: q to quit without saving

At last I want Vim to exit without saving when exiting :q. It annoys me when Vim prompts me to save when I quit the document when I try to :qchange it . Whenever I want to keep changes, I save them. add! The :q!position sits awkwardly on my UK keyboard. How ca

Vim map: q to quit without saving

At last I want Vim to exit without saving when exiting :q. It annoys me when Vim prompts me to save when I quit the document when I try to :qchange it . Whenever I want to keep changes, I save them. add! The :q!position sits awkwardly on my UK keyboard. How ca

Python - read audio data without saving to file

Sean Zhang I'm sending audio data between the browser and my AWS Lambda function, but I find myself doing an intermediate step to save it to a file for functional purposes. Here is my code that works now: wavio.write(file="out.wav", data=out_file, rate=16000,