у меня такая проблема, как можно закрыть открытый файл.
import win32api import win32con import random, datetime, time, calendar import sys import wx import time class CanvasPanel(wx.Panel): now_date = datetime.date.today() path = str(now_date) + '.txt' document = open(path,'w') """ create a panel with a canvas to draw on""" def __init__(self, parent): wx.Panel.__init__(self, parent, -1) wx.EVT_SIZE(self, self.create_canvas) self.timer = wx.Timer(self) self.timer.Start(30) self.Bind(wx.EVT_TIMER, self.onTimer) def onTimer(self, event): self.canvas.Clear() for i in range(65,91): if win32api.GetAsyncKeyState(ord(unichr(i))): time.sleep(0.3) self.document.write('hello') print unichr(i), #здесь я хочу закрыть document с self.document.close() но она выдал такая ошибка ''' Traceback (most recent call last): File "C:\Users\MaxWell\Desktop\_\P - копия\duck\duck.py", line 23, in onTimer self.document.write('hello') ValueError: I/O operation on closed file ''' def create_canvas(self, event=None): """create the paint canvas""" self.canvas = wx.ClientDC(self) app = wx.App(0) frame = wx.Frame(None, -1, "Title", size=(500, 400)) CanvasPanel(frame) frame.Show(True) app.MainLoop()
Заранее Спасибо!!!