I'm using watchdog to monitor some folders and create a csv file to see when a file is created and modified. Now, I got a dataframe like this from the csv:
full_path name created modified
0 C:\T1\1.txt 1.txt 14:04:30 NaN
1 C:\T1\1.txt 1.txt NaN 14:04:30
2 C:\T1\T2\1.txt 1.txt 14:10:30 NaN
3 C:\T1\T2\1.txt 1.txt NaN 14:10:30
4 C:\T1\T2\T3\1.txt 1.txt 14:15:30 NaN
5 C:\T1\T2\T3\1.txt 1.txt NaN 14:15:30
6 C:\T1\T2\T3\T4\1.txt 1.txt 14:20:30 NaN
7 C:\T1\T2\T3\T4\1.txt 1.txt NaN 14:20:30
8 C:\T1\2.txt 2.txt 14:25:30 NaN
9 C:\T1\2.txt 2.txt NaN 14:25:30
10 C:\T1\T2\2.txt 2.txt 14:30:30 NaN
11 C:\T1\T2\2.txt 2.txt NaN 14:30:30
12 C:\T1\T2\T3\2.txt 2.txt 14:35:30 NaN
13 C:\T1\T2\T3\2.txt 2.txt NaN 14:35:30
14 C:\T1\T2\T3\T4\2.txt 2.txt NaN 14:40:30
Watchdog, usually gives two timestamps when a file is moved to another folder (created and modified), but somehow for the last file when it moved to T4 folder it gave only the modified timestamp. I use this code to convert this dataframe in only two rows and I put the timestamps for each folder in other columns:
m0 = (df["full_path"].ne(df["full_path"].shift(1, fill_value=df["full_path"].iloc[0])) & df["name"].eq(df["name"].shift(fill_value=df["name"].iloc[0])))
m1 = df["full_path"].eq(df.loc[df["full_path"].str.rsplit("\\", 2).str[-2] == 'T1', 'full_path'])
m2 = df["full_path"].eq(df.loc[df["full_path"].str.rsplit("\\", 2).str[-2] == 'T2', 'full_path'])
m3 = df["full_path"].eq(df.loc[df["full_path"].str.rsplit("\\", 2).str[-2] == 'T3', 'full_path'])
m4 = df["full_path"].eq(df.loc[df["full_path"].str.rsplit("\\", 2).str[-2] == 'T4', 'full_path'])
df['T1'] = np.where(m0 & m1, df['created'], "")
df['T2'] = np.where(m0 & m2, df['created'], "")
df['T3'] = np.where(m0 & m3, df['created'], "")
df['T4'] = np.where(m0 & m4, df['created'], "")
df = df.groupby(['name'], sort=False).agg({'full_path':'last','created':'first', 'modified':'last','T1':'first', 'T2':lambda x: ' '.join(set(x)), 'T3':lambda x: ' '.join(set(x)), 'T4':'last'}).reset_index()
And it gives me a dataframe like this one:
full_path name created modified T1 T2 T3 T4
0 C:\T1\T2\T3\T4\1.txt 1.txt 14:04:30 14:20:30 14:04:30 14:10:30 14:15:30 14:20:30
1 C:\T1\T2\T3\T4\2.txt 2.txt 14:25:30 14:40:30 14:25:30 14:30:30 14:35:30 NaN
How can I modify my code to detect for example for the file 2.txt when it moved to folder T4, if the "created" column in NaN, get the timestamp from the "modified" column, and have a dataframe like this one:
full_path name created modified T1 T2 T3 T4
0 C:\T1\T2\T3\T4\1.txt 1.txt 14:04:30 14:20:30 14:04:30 14:10:30 14:15:30 14:20:30
1 C:\T1\T2\T3\T4\2.txt 2.txt 14:25:30 14:40:30 14:25:30 14:30:30 14:35:30 14:40:30