Вам необходимо закрыть файл до того, как в системе произойдет обновление файла.Вы не можете прочитать или записать открытый файл.
def log(self, exampletext):
with open(self.filename) as fn:
fn.write(exampletext)
В этом примере файл будет автоматически закрыт после того, как будет написана строка.
Вот что я использую для создания своего собственногофайлы журнала с тем же конечным результатом, который вы ищете.
class Log: #class to write to log file with time stamps
def __init__(self):
import os, traceback, time
self.traceback = traceback
self.os = os
self.time = time
if getattr(sys, 'frozen', False): #windows path fix
self.exe = self.os.path.dirname(sys.executable)
elif __file__:
self.exe = self.os.path.dirname(__file__)
if not os.path.exists(os.path.dirname(str(os.environ['USERPROFILE'])+"\\Documents\\AppName\\")):
os.makedirs(str(os.environ['USERPROFILE'])+"\\Documents\\AppName\\")
self.fname = str(os.environ['USERPROFILE'])+"\\Documents\\AppName\\debug.log"
self.logfile = None
def error(self, error):
exc_type, exc_obj, exc_tb = sys.exc_info()
trace_stack = self.traceback.extract_tb(exc_tb)[-1]
trace_format = "Error in file "+str(trace_stack[0])+"\r on line "+str(trace_stack[1])+", from module '"+str(trace_stack[2])+"'\r "+str(trace_stack[3])
try:
self.logfile = open(self.fname, "a+")
except:
self.logfile = open(self.fname, "w+")
strtime = str(self.time.strftime("%d-%m-%Y,(%z),%H:%M:%S"))
self.logfile.write("error: %s, %s, %s\r" %(strtime, error, trace_format))
self.logfile.close()
self.logfile = None
def log(self, log):
try:
self.logfile = open(self.fname, "a+")
except:
self.logfile = open(self.fname, "w+")
strtime = str(self.time.strftime("%d-%m-%Y,(%z),%H:%M:%S"))
self.logfile.write("log: %s, %s\r" %(strtime, log))
self.logfile.close()
self.logfile = None
, и вот как я использую его в моем приложении
try:
self.log.log("This is a standard log")
except Exception as err:
exc_type, exc_obj, exc_tb = sys.exc_info()
self.log.error("create_options failed\n%s, %s, %s, %s" %(err, exc_type, exc_obj, traceback.print_tb(exc_tb)))
РЕДАКТИРОВАТЬ : дляБолее быстрый (простой) журнал - вот как я бы это сделал.
#logger.py
class log:
def __init__(self, message):
f = open("default.log", "a+")
f.write(message+"\r")
f.close()
#maincode.py
from logger import log
for i in range(10):
log("Hello World %s" %i)
Вы можете просто заменить все операторы печати на операторы журнала.