deleting results after completion of code's execution PYTHON [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Currently I am working on a project in which I need to save n number of images (to be used in the program's scope). Since the number of images to be saved is dynamic, it may end up exhausting the whole space which i have for my project.
I wanted to know that can there be something added to my code so that after 100% completion of my code the images get automatically deleted as I do not need them after the code's execution.
How can this be done?
I need to save images as they are passed as an argument to one of my functions inside my code. If you know how can I pass image without saving it to my function then please comment here

might be an idea to delete the files immediately after you've done the code you need to do i.e
import os
# Open image
# Manipulate image
os.remove(path_to_image)

Keep track of all the image files you're creating, then delete them in a finally block to ensure they'll be deleted even if an exception is raised.
import os
temp_images = []
try:
# ...do stuff
# ...create image at path_to_file
temp_images.append(path_to_file) # called multiple times
# ...other stuff
finally:
for image in temp_images:
os.remove(image)

Related

Detecting changes in a txt file [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 months ago.
Improve this question
I am a beginner python programmer and I am wondering if there is any way to detect a change in a txt file on windows. Any suggestion is appreciated.
There are many ways to go with it :
You can for example check the last modification date of the file every few seconds with os.path.getmtime(path), when the date change you know the file was edited.
You can also use some form of checksum (generate md5 hash of a file) on the file and check every few seconds if the checksum change (can get slow on big files since the checksum require to read the entire file)
You can also listen for signals send by windows directly and execute an event handler when you get a signal, this is harder to implement but by far the cleanest way to do it. (Edit, this seems to be what #martin kamau suggest in his answer)
Probably many more way that I can't think of right now...
To watch for file changes in a file, you can use the following code:
import time
import fcntl
import os
import signal
filename = "nameofthefile"
def handler(signum, frame):
print "File %s modified" % (FNAME,)
I found this code here.

Get last execution time of script python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Im writting a script in Python that takes as an input a timestamp. For the first execution, it should be something as the now() function. However, for further executions, the input parameter should be the last execution time of the same script. This is being done to avoid getting duplicates results.
Can anyone give me a clue please?
As far as I know, there is no "last executed" attribute for files. Some operating systems have a "last accessed" attribute, but even if that's updated on execution, it would also be updated any time the file was read, which is probably not what you want.
If you need to be able to store information between runs, you'll need to save that data somewhere. You could write it to a file, save it to a database, or write it to a caching service, such as memcached.

How to replace while loop with Tkinter loop [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I was wandering if anyone was able to help me properly add Tkinker into my existing script?. The Tkinker code works fine outside of the current script as does the script without the Tkinter code, but I am having trouble merging it together. been doing alot of searching and i cant seem to fine the answer i am looking for. Here is the link to my script:
https://github.com/Octane70/Code/blob/master/Garden/Garden_v0.3.py
Line 50 #GUI_window is the code i am trying to add.
Thanks
You need to call root.mainloop() exactly once. You need to convert your while True loop into a function, and at the end of the function you need to call itself again with after. This function should also update the GUI, though you could put that in a separate function if you wish.
You do not want to call your gui_display function more than once. As it stands now, you are creating six new widgets every second. Instead, you want to create them once and then update them every second.

Python memory leak: do I need to delete? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I've got a python script that is slowly consuming all of my memory (48GB). If I recall, python will perform garbage collection so there is no need for me to cleanup after myself?
for example:
class data_store:
dat1={}
dat2={}
dat3={}
class myclass ():
def mem_func(self):
self.x = data_store()
self.x.dat1 = (lots of data)
self.x.dat2 = (lots of data)
y = x.dat1 + 1
...
Most of my data is stored in data_store() temporarily before it is written out to files. I would think that this would be the source of the leak. Everytime mem_func() is called a new data_store() object is created and assigned to self.x. I assume that the old data_store() object would now be a candidate for the GC to delete. In addition, I would assume that y also be able to be deleted after mem_func completes.
The only other thing I can think of is that I am creating figures with matplotlib and saving them to a file. That is all done in one function but perhaps I need to delete the figure properly. Also, I have a sqlite db that is open the whole time where I am writing data but that is not alot of data. The image is much bigger.
You need to remember that GC only collects data that no pointer (variable) is pointing at it. In other words, as long as the memory is accessible via your variables, it won't be collected/freed.
So you need to assing None to the variables you don't need any more, or assign new data to the same variable names, if you don't need them any more.

Display a file to the user with Python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I am trying to find a way to display a txt/csv file to the user of my Python script. Everytime I search how to do it, I keep finding information on how to open/read/write etc ... But I just want to display the file to the user.
Thank you in advance for your help.
if you want the file to open with its associated default program, use startfile.
os.startfile("path/to/file") # may only work on Windows
It really depends what you mean by "display" the file. When we display text, we need to take the file, get all of its text, and put it onto the screen. One possible display would be to read every line and print them. There are certainly others. You're going to have to open the file and read the lines in order to display it, though, unless you make a shell command to something like vim file.txt.

Categories

Resources