簡體   English   中英

如何在程序重新運行期間停止重寫日志數據 / Python

[英]How stop re-writing log data during program re-run / Python

我在記錄時遇到問題。 每次我重新運行我的程序時,它都會覆蓋文件中的日志數據,因為我還需要存儲以前的數據。 當有文件時,我創建了 if 語句,它並沒有像我想的那樣創建新的語句,但它並沒有解決我的問題。 也許有人知道這個問題? 先感謝您!

from tkinter import *
from tkinter import filedialog
import easygui
import shutil
import os
from tkinter import filedialog
from tkinter import messagebox as mb
from pathlib import Path
import logging
from datetime import date


def open_window():
    read=easygui.fileopenbox()
    return read

#logging config

if Path('app.log').is_file():
    print ("File exist")
else:
    logging.basicConfig(filename='app.log', filemode="w", format='%(name)s - %(levelname)s - %(message)s ')
    print ("File doesn't exist and will be created")


LOG_for="%(asctime)s, log content: %(message)s"

logger=logging.getLogger()
logger.setLevel(logging.DEBUG)


# Function for opening the
# file explorer window
def browseFiles():
    filename = filedialog.askopenfilename(initialdir = "/",
                                          title = "Select a File",
                                          filetypes = (("Text files",
                                                        "*.txt*"),
                                                       ("all files",
                                                        "*.*")))
      
    # Change label contents
    label_file_explorer.configure(text="File Opened: "+filename)
      
# move file function
def move_file():
    source = open_window()
    filename = os.path.basename(source)
    destination =filedialog.askdirectory()
    dest = os.path.join(destination,filename)
    if(source==dest):
        mb.showinfo('confirmation', "Source and destination are same, therefore file will be moved to Home catalog")
        newdestination = Path("/home/adminas")
        shutil.move(source, newdestination)
        logging.shutdown()
        #current_time()
        logging.basicConfig(filename='app.log', filemode="w", format=LOG_for) 
        logging.info('File was moved to' + newdestination)
        
    else:
        shutil.move(source, destination)  
        mb.showinfo('confirmation', "File Moved !")
        #current_time()
        logging.basicConfig(filename='app.log', filemode="w", format=LOG_for)
        logging.info('File was moved to' + destination) 
                                                                                                  
# Create the root window
window = Tk()
  
# Set window title
window.title('File Explorer')
  
# Set window size
window.geometry("400x400")
  
#Set window background color
window.config(background = "white")
  
# Create a File Explorer label
label_file_explorer = Label(window,
                            text = "File Explorer using Tkinter",
                            width = 50, height = 4,
                            fg = "blue")
  
      
button_explore = Button(window,
                        text = "Browse Files",
                        command = browseFiles)

button_move = Button(window,
                        text = "Move File",
                        command = move_file)     
  
button_exit = Button(window,
                     text = "Exit",
                     command = exit)
  
# Grid method is chosen for placing
# the widgets at respective positions
# in a table like structure by
# specifying rows and columns
label_file_explorer.grid(column = 1, row = 1)

button_move.grid(column = 1, row = 2)
  
button_exit.grid(column = 1,row = 3)
  
# Let the window wait for any events
logging.shutdown()
window.mainloop()

但是在 shell 中,它可以正確打印,但是每次運行程序時,它都會一遍又一遍地覆蓋新數據,例如,重新運行后它會消失並被新數據取代:

2021-05-02 11:04:15,384, log level: INFO, log content: File was moved to/home/adminas/Documents

不太明白你的問題。 但是,如果您不想覆蓋日志文件,請將文件模式更改為'a' ,這會將filemode新記錄到您的日志文件中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM