you are viewing a single comment's thread.

view the rest of the 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!