Wait for the file to be fully copied before parsing it

This commit is contained in:
2022-01-12 10:13:01 +10:00
parent d93f0869a7
commit 23f68f19e8
3 changed files with 32 additions and 16 deletions

View File

@@ -12,10 +12,10 @@ import watchdog.events
import watchdog.observers
DATE_FORMAT = "%d/%m/%Y"
#WATCH_DIR = os.path.dirname(os.path.realpath(__file__))
WATCH_DIR = '/mnt/data/'
PATTERN = '*.qfx'
BACKUP_DIR = 'Imported'
CONVERTED_DIR = 'Converted'
class Handler(watchdog.events.PatternMatchingEventHandler):
@@ -69,25 +69,40 @@ class Handler(watchdog.events.PatternMatchingEventHandler):
return path
def on_created(self, event):
qfx = OfxParser.parse(open(event.src_path, encoding="latin-1"), fail_fast=False)
statement = Handler.get_statement_from_qfx(qfx)
print('File found: {}'.format(event.src_path))
historicalSize = -1
while (historicalSize != os.path.getsize(event.src_path)):
historicalSize = os.path.getsize(event.src_path)
print('waiting....')
time.sleep(1)
print("file copy has now finished")
with open(event.src_path, 'r') as file:
qfx = OfxParser.parse(file, fail_fast=False)
statement = Handler.get_statement_from_qfx(qfx)
path = Path(event.src_path)
path.resolve()
path = Path(event.src_path)
path.resolve()
out_file = str(path.parent / ('converted' + path.stem + '.csv'))
Handler.write_csv(statement, out_file)
converted_dir = path.parent / CONVERTED_DIR
if not converted_dir.exists():
converted_dir.mkdir()
#Now move the input file to backup
archive_file_dir = path.parent / BACKUP_DIR
archive_file = (path.stem + '{:04d}' + path.suffix)
destination = Handler.unique_path(archive_file_dir, archive_file)
out_file = str(path.parent / CONVERTED_DIR / ('converted' + path.stem + '.csv'))
Handler.write_csv(statement, out_file)
if not archive_file_dir.exists():
archive_file_dir.mkdir()
#Now move the input file to backup
archive_file_dir = path.parent / BACKUP_DIR
archive_file = (path.stem + '{:04d}' + path.suffix)
destination = Handler.unique_path(archive_file_dir, archive_file)
if not destination.exists():
path.replace(destination)
if not archive_file_dir.exists():
archive_file_dir.mkdir()
if not destination.exists():
path.replace(destination)
if __name__ == "__main__":