python


Python3: How to save data, when the destructor is called


I actually have a question on Python3. I want to save the attributes of a class in a file, when the destructor is called. How can I do that?
I tried to do it like this:
def __del__:
file = open("test.dat", "w")
file.write("Hello")
file.close()
That code doesn't work. I've already read, that you shouldn't use it, but I actually didn't find a alternative which works. Can anybody help me?
Thanks in advance!
to use that code it needs to be part of a class:
class Test():
def __init__(self):
self.data = "Hello"
def __del__(self):
with open('text.txt', mode='w') as file:
file.write(self.data)
if __name__ == '__main__':
def something():
obj = Test() # obj is deleted when function exits as it becomes out of scope
something()
obj = Test()
del obj # also works because we explicitly delete the object
note that while this method does work, it cannot always be relied upon to always be executed, for example if a sys.exit is called

Related Links

How to make a response on the web using MQTT and Flask?
Four-of-a-Kind Dice Game
Python subprocess - how to redirect only input
Calculation Server Architecture
PyAutoGui crashes when if/then statements are included
Disallowed Host at Django
Cannot import PyQtGraph because packages PySide, PyQt4, or PyQt5 can't be imported?
BeautifulSoup4 not working on my hosting server
Argument parser error: too few arguments
from .forms import RegisterForm error
How to automate grabcut algorithm in opencv python?
2-dimensional binning with Pandas
how to get the differences in a list
I need this squares to 'fall' or be positioned lower when there's nothing under them
Python: Extract XML texts except under certain tags
Using functions in different files

Categories

HOME
makefile
sql-server
ember.js
ssas-2012
crate
reverse-engineering
oauth
checkbox
filterrific
uibutton
angular2-routing
wxwidgets
visualization
quill
shipping
eclipse-cdt
pdo
atlassian-plugin-sdk
kadanes-algorithm
sonarqube-msbuild-runner
os161
go-cd
codelite
unpack
spring-data-neo4j
xilinx-ise
ggmap
multichoiceitems
aurelia-binding
pep8-assembly
twitter-bootstrap-2
strophe
expand
hotmail
division
pace
color-scheme
boilerplate
mongoexport
active-model-serializers
polyfills
lexical-analysis
bayesian-networks
control-flow-graph
google-account
lftp
matlab-cvst
lumen-5.3
aqgridview
tuleap
gd
livescribe
chrome-remote-desktop
struts-layout
iron.io
payu
xib
icefaces
audioqueue
software-product-lines
django-filer
multiple-file-upload
qregexp
linkageerror
selecteditem
asp.net-mvc-partialview
proxygen
flexjson
eml
cmocka
persistence.xml
hornetq
affix
vips
retro-computing
python-winshell
ytplayerview
gray-code
httpie
jython-2.7
ejabberd-saas
pisa
cpu-speed
scala-2.11
dmp
level
openkinect
winrt-httpclient
shiva3d
mozart-mvc
bulbs
linkedhashset
symfony-2.0
deploying
subtract
gjs
objective-c-protocol
driving-directions
executable-format
authenticode
windows-live-id
email-spec
hadoop-plugins
rtsp-client
msf

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App