how to close a file when open with os.close() - python

I have a piece of code which would save a file and after once clicked the tkinter button for saving it, python saves it every 5 minutes. But you could also open the saved file: so I need a piece of code which first tries to close the file (if open) and then save it again. This is my code:
def save_changes():
# first close the saved_file
wb.save(saved_file)
cur_time = datetime.datetime.now().strftime("%H.%M.%S")
saved_label.config(text="laatst opgeslagen op:\n" + cur_time)
saved_label.after(300000, save_changes)

Related

Opening and closing a file while logging data

So, I'm logging temperature and humidity data from a DHT22 hooked up to the GPIO on a raspberry pi. It logs everything correctly - but I can only see the updated log after I stop logger.py running.
I think the problem is that I'm not closing the file after writing to it - but I'm not sure. Can I just add a f = open(xxx) and f.close() to the loop so that it 'saves' it everytime it logs?
import os
import time
import Adafruit_DHT
DHT_SENSOR = Adafruit_DHT.DHT22
DHT_PIN = 4
try:
f = open('/home/pi/temphumid/log.csv', 'a+')
if os.stat('/home/pi/temphumid/log.csv').st_size == 0:
f.write('Date,Time,Temperature,Humidity\r\n')
except:
pass
while True:
humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
if humidity is not None and temperature is not None:
f.write('{0},{1},{2:0.1f}*C,{3:0.1f}%\r\n'.format(time.strftime('%m/%d/%y'), time.strftime('%H:%M:%S'), temperature, humidity))
else:
print("Failed to retrieve data from humidity sensor")
time.sleep(60)
expected:
log.csv is updated, so that if I use tail log.csv I can see the up to date data.
actual:
log.csv doesn't update until I stop logger.py from running (using sigint from htop as it is currently run as a cronjob on boot).
Every time we open a file we need to close it to push the output to disk:
fp = open("./file.txt", "w")
fp.write("Hello, World")
fp.close()
To avoid calling the close() method every time, we can use the context manager of the open() function, which will automatically close the file after exiting the block:
with open("./file.txt", "w") as fp:
fp.write("Hello, World")
We do not need to call here the close method every time to push the data into the file.
Write data to the file and hit file.flush() and then do file.fsync() which writes the data to the disk and you'll even be able to open file using different program and see changes at the real time.

Python Threading.timer()

I'm trying to perform a function after a time interval.
The time interval is controlled from Ftp server Notepad file.
The user updates notepad file which updates the time interval after some time.
But it is not working it reads the data from the notepad file on startup and the threading.timer use old time it doesn't update Code even the data in notepad file is changed by us
def Dataflow():
try:
ftp = FTP('Domain')
ftp.login('username', 'password')
ftp.cwd('/directory where notepad file is saved')
server_file='control.txt' #The Text file that will control the data flow
local_file=r'C:\Users\Public\flow.txt' #The Local location of file which will catch data
ftp.retrbinary('RETR ' + server_file , open(local_file, 'wb').write) # To download the Dataflow rate from the notepad in ftp server
local_file = open(r'C:\Users\Public\flow.txt','r')
temp_store = int(local_file.read(20)) #storing the data of notepad file in variable
local_file.close(); ftp.close()
return temp_store
except:
pass
def function():
do something
time = threading.timer(Dataflow(), function)
time.start()

GUI File Picker

I am trying to use PYQT5 file picker, but there is something I am missing. I'm having two problems. the first is when the file dialog box opens and I choose a file. the whole program crashed and I get AttributeError: 'list' object has no attribute 'seek' and QWaitCondition: Destroyed while threads are still waiting that show in the terminal. The second is when I hit cancel on the file dialog the whole program crashes and it says nboundLocalError: local variable 'newdata' referenced before assignment and QWaitCondition: Destroyed while threads are still waiting. What I would like is to be able to have the dialog pop up and choose the file and then and then have the contents of that file be loaded into that variable. I'm not sure what is going wrong. I have posted my current code below. Any advice or help is greatly appreciated.
def open(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
try:
fileToOpen = QFileDialog.getOpenFileNames(self,"Open File", "","All Files (*);;Python Files (*.py)", options=options)
except:
pass
pdb.set_trace()
if fileToOpen:
with ZipFile(fileToOpen, 'r') as myzip:
json_data_read = myzip.read('digest.json')
newdata = json.loads(json_data_read)
Functions.Loads = newdata[1]
getOpenFilename returns a tuple. You want the second return value so call it like
fileToOpen, _ = getOpenFilename(...)
It's because pyqt5 calls getOpenFilenameAndFilter: http://pyqt.sourceforge.net/Docs/PyQt5/pyqt4_differences.html#qfiledialog
The second problem is because you don't init newData if there is no filename.

Open a text file with python as a form and know when it closes

I want to make a python script that opens a text file that the user can update before continuing (for configurations) and then continue the script once the text editor closes. Is there something in python that will allow me to do this?
on windows using notepad:
import os
cf = "config.txt" # or full path if you like
if not os.path.exists(cf):
f = open(cf,"w"); f.close() # create to avoid notepad message
os.system("notepad "+cf) # call waits until user closes the file
# now the file has been edited by the user

Python Doesn't Release File/Cannot Save File (xlutils)

So I have a XLS file that is being accessed via the xlutils library. When my program is finished doing its process, its supposed to delete the original file and rename the temporary file to the original file. The data in the excel file is being input a website and data from the website is being extracted and then written to the excel sheet. All this is fine, however, when its done its having an issue renaming the temp file.
The temp file is simply a copy made via the following function:
def rename(self, fpath):
tempf=os.path.splitext(fpath)[0]
tempf=tempf + "-output" + ".xls"
shutil.copyfile(fpath,tempf)
return tempf
I have created a function which is called when the for loop for inputting data and extracting is finished:
def allDone(self, event):
dlg = wx.MessageBox("All done!", "Ask Alfred", wx.OK | wx.ICON_INFORMATION)
os.unlink(self.fpath)
os.rename(self.temp, self.fpath)
Using Process Explorer, it shows that the temp file is still open in python.exe. I don't know how to close the file and free memory since it is being opened by using:
rbook=open_workbook(file)
sheet = rbook.sheet_by_index(0)
where file will simply be replaced with whatever self.temp is.
When a GO button is pushed the following happens:
def onGo(self, event):
fpath=self.pathBox
fpath=fpath.GetValue()
self.fpath=fpath
temp=myClass.rename(fpath)
self.temp=temp
openFile(temp)
def rename(self, fpath):
tempf=os.path.splitext(fpath)[0]
tempf=tempf + ".alf"
shutil.copyfile(fpath,tempf)
return tempf
So the name of the file which is in a SearchCtrl called "pathBox" is obtained and a temp copy is made and then renamed.

Categories