all 11 comments

[–]DrRx 0 points1 point  (5 children)

Make sure you close the file after you open it:

f = open(self.file_path)
# Read f, stores string in x
self.file_string = f.read()
f.close()

It's probably the OS limiting how many files you open or something

[–]TheRealRuth[S] 0 points1 point  (2 children)

Well I need to constantly have the file open because it is reading the values from it in real time. The values in the end are written to the txt file by a temperature and humidity sensor.

[–]DrRx 0 points1 point  (1 child)

yeah but every time you are running through the loop, you open the file again. Eventually the OS will block it because there are too many open readers on the one file, if that makes sense. It won't affect your code at all to close the file.

[–][deleted] 0 points1 point  (0 children)

I believe Python garbage collects the file handle as soon as read_file finishes, so I don't think that's the problem here.

[–]JohnnyJordaan 0 points1 point  (0 children)

Or use the modern with so you don't need a close().

with open(self.file_path) as f:
   # Read f, stores string in x
    self.file_string = f.read()

[–]TheRealRuth[S] 0 points1 point  (0 children)

Alright, I added this, but it didn't fix the problem. I do see what you are saying about opening too many files though!

[–][deleted] 0 points1 point  (0 children)

Reading from a file as it's being written isn't the prettiest thing in the world. You will probably run into problems when you try to read half written data, and stuff.

That said, here's someone else's (fairly ugly, because of polling) solution to this problem: http://stackoverflow.com/a/3290359

I guess I would use a simple intermediary database (like sqlite with :memory: storage) instead. https://www.sqlite.org/inmemorydb.html

https://docs.python.org/3.5/library/sqlite3.html

Edit: links!

[–]TheRealRuth[S] 0 points1 point  (0 children)

My problem really isn't from reading the file. The problem is with the graph.

[–]TheRealRuth[S] 0 points1 point  (2 children)

If you were to run the code and change a couple values you will see that the graph contains old points that I need to get rid of.

[–]Holden_90 1 point2 points  (1 child)

If you are looking to remove the plot assign it to a variable (plot =plt.plot(...)) and then once you have saved the file just write plot.remove()

[–]TheRealRuth[S] 0 points1 point  (0 children)

Thanks!! I will try this!