Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在程序重新运行/Python期间停止重新写入日志数据_Python_Audit Logging - Fatal编程技术网

如何在程序重新运行/Python期间停止重新写入日志数据

如何在程序重新运行/Python期间停止重新写入日志数据,python,audit-logging,Python,Audit Logging,我在日志记录方面遇到了问题。每次我重新运行程序时,它都会覆盖文件中的日志数据,因为我还需要存储以前的数据。我已经创建了if语句,当有文件时,它并没有像我想的那样创建一个新的,但它并没有解决我的问题。也许有人知道这个问题?提前谢谢你 from tkinter import * from tkinter import filedialog import easygui import shutil import os from tkinter import filedialog from tkinte

我在日志记录方面遇到了问题。每次我重新运行程序时,它都会覆盖文件中的日志数据,因为我还需要存储以前的数据。我已经创建了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'
,这样会将新日志附加到日志文件中。

不太了解您的问题。但是,如果您不想覆盖日志文件,请将更改为
'a'
,这样会将新日志附加到您的日志文件中。

感谢@jiang的回复。问题是,每次我运行程序并对移动文件执行某些操作时,我以前在日志文件中的记录都会消失,并被新记录替换。我想实现的是将所有程序使用数据保存在日志文件中这就是filemode的功能,
'w'
模式将覆盖,
'a'
将追加感谢@jiang,以前它不起作用,我尝试了其他选项。但现在一切顺利了,谢谢!谢谢@jiang的回复。问题是,每次我运行程序并对移动文件执行某些操作时,我以前在日志文件中的记录都会消失,并被新记录替换。我想实现的是将所有程序使用数据保存在日志文件中这就是filemode的功能,
'w'
模式将覆盖,
'a'
将追加感谢@jiang,以前它不起作用,我尝试了其他选项。但现在一切顺利了,谢谢!