Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88835

How can I write directly to an excel file and is there a better way to get next available row?

$
0
0

I'm making a file logger that logs every event (an event being a file/directory being added, deleted, modified, or moved) into an excel file. The criteria being recorded are the time it was done, the event type, the source path, and the destination path (destination path is only for files being moved).

It should basically record all of this in the next available row:

NextAvailRow = df.index.max()+1

The two questions I have is that

A. For some reason the code works with a dataframe but can't seem to write directly to the excel file. What am I doing wrong (do I have to use xlsxwriter?)

B. Is there a better solution than my NextAvailRow variable?

import time
from datetime import datetime

#Watchdog
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler

#Writing to Excel.
import pandas as pd
import numpy as np

#--------------------------------------------------------------------------------------

df = pd.DataFrame(columns = ["Time", "Event Type", "Source Path", "Destination Path"])
df.to_excel('Watchdog.xlsx', sheet_name = "Log", index=True)

log = pd.read_excel('Watchdog.xlsx', sheet_name='Log')

#--------------------------------------------------------------------------------------

#Create Event Handler: Object that will be notified when something happens on the file system monitered.

if __name__ == "__main__":
    patterns = "*"
    ignore_patterns = ""
    ignore_directories = False
    case_sensitive = True
    my_event_handler = PatternMatchingEventHandler(patterns, ignore_patterns, ignore_directories, case_sensitive)

#--------------------------------------------------------------------------------------

#Event Handler
def on_any_event(event):
        NextAvailRow = df.index.max()+1
        if event.event_type == "moved":
            log.at[NextAvailRow, 'Time'] = datetime.now()
            log.at[NextAvailRow, 'Event Type'] = event.event_type
            log.at[NextAvailRow, 'Source Path'] = event.src_path
            log.at[NextAvailRow, 'Destination Path'] = event.dest_path
        else:
            log.at[NextAvailRow, 'Time'] = datetime.now()
            log.at[NextAvailRow, 'Event Type'] = event.event_type
            log.at[NextAvailRow, 'Source Path'] = event.src_path

my_event_handler.on_any_event = on_any_event

#--------------------------------------------------------------------------------------

#Observer: Monitor filesystem.
path = "."
go_recursively = True
my_observer = Observer()
my_observer.schedule(my_event_handler, path, recursive = go_recursively)

#--------------------------------------------------------------------------------------

#Start Observer

my_observer.start()
try:
    while True:
        time.sleep(1)
except KeyboardInterrupt:
   my_observer.stop()
    my_observer.join()

Thank you.


Viewing all articles
Browse latest Browse all 88835

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>