-
Notifications
You must be signed in to change notification settings - Fork 0
/
Folder_watchdog.py
70 lines (58 loc) · 2.22 KB
/
Folder_watchdog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# SOURCE https://medium.com/@BetterEverything/automate-incoming-file-processing-with-python-df7daca0e7ff
# TODO merge this with larger deduper and watchdog.
"""
The directory watcher that we will create will consist of 4 systems:
1. a system to repeat a process every X seconds
2. a system to check whether there are files in a directory
3. a system that processes the files (this is specific to the use case)
4. a system that moves a file out of the watched directory
"""
import time
import os
import shutil
from os import environ, path
from dotenv import load_dotenv
import TODO_scanner
load_dotenv()
def main():
"""
runs each time Folder_watchdog.py runs
:return:
"""
csv_order_collection()
TODO_scanner()
# TODO need to adjust split as we are missing first few words in /data
# TODO error "UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa6 in position 343: invalid start byte"
def csv_order_collection():
"""
using os.listdir iterate through PATh to parse order number from filename
:return:
"""
while True:
files = os.listdir(directory_path)
for file in files:
filepath = directory_path + file
##BEGIN: USE CASE SPECIFIC##
ordernumber = file[5:].split(".csv")[0]
orderlines = []
with open(filepath, "r") as f:
for line in f:
line = line.strip()
orderlines.append("{},{}\n".format(ordernumber, line))
# TODO move path to .env file
# TODO remove duplicated path below
with open("./data/collected_orders.csv", "a") as f:
for line in orderlines:
f.write(line)
##END: USE CASE SPECIFIC##
destination_file = destination_path + file
shutil.move(filepath, destination_file)
time.sleep(10)
if __name__ == "__main__":
USER_NAME = environ.get("USER_NAME")
directory_path = f"/Users/" f"{USER_NAME}/sbox/PycharmProjects/SQL_Template/data/"
destination_path = f"/Users/{USER_NAME}/sbox/PycharmProjects/SQL_Template/data/processed/"
# Find .env file
basedir = path.abspath(path.dirname(__file__))
load_dotenv(path.join(basedir, ".env"))
main()