Python Forum

Full Version: Monitoring a Directory for new mkv and mp4 Files
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,

i wanted to code a Monitor that observes a Folder and it`s subfolders for new mkv and mp4 Files. But when i add a File to the Folder the Routine always shows me all files it has found and not just the new onces.

i tried it with
new_series = [item for item in resseries if item not in old_series]
where new_series should contain only the new files, but i have no idea why it always shows the whole content of old_series once i copy a new file to the folder.

I´ve attached my whole code, maybe someone hass an Idea

import os,re,time
from os import walk
from time import sleep
import subprocess,os,sys,time
from datetime import datetime	
import requests

# folder path
dir_pathmovies = r'T:\\latest Movies\\_test\\'
dir_pathseries = r'T:\\Series\\test\\'

# list to store files
result = []
resmovies = []
resseries = []
newmovies = []
newseries = []
old_series = []
old_movies = []

def monitor():
	
	global result,resmovies,resseries,dir_pathmovies,dir_pathseries,old_series,old_movies
	mkv = []
	mp4 = []

	# Iterate directory - Initial Setup
	for (dir_pathseries, dir_names, file_names) in walk(dir_pathseries):
		result.extend(file_names)
	mkv = [x for x in result if ".mkv" in x]
	mp4 = [x for x in result if ".mp4" in x]
	old_series.append(mkv)
	old_series.append(mp4)
	result.clear
	mkv = []
	mp4 = []
	for (dir_pathmovies, dir_names, file_names) in walk(dir_pathmovies):
		result.extend(file_names)
	mkv = [x for x in result if ".mkv" in x]
	mp4 = [x for x in result if ".mp4" in x]
	old_movies.append(mkv)
	old_movies.append(mp4)
	
	try:
		while True:
			scanseries() 
			new_series = [item for item in resseries if item not in old_series]
			print (new_series)
			old_series=resseries        # copy new found names to the old_list

			for remaining in range(10, 0, -1):
				sys.stdout.write("\r")
				sys.stdout.write("{:2d} seconds remaining before new DIRSCAN.".format(remaining))
				sys.stdout.flush()
				time.sleep(1)
            
	except KeyboardInterrupt:
		exit()	



def scanseries():
	global dir_pathseries,dir_pathmovies,resseries
	resseries = []
	result = []
	for (dir_pathseries, dir_names, file_names) in walk(dir_pathseries):
		result.extend(file_names)
	mkv = [x for x in result if ".mkv" in x]
	mp4 = [x for x in result if ".mp4" in x]
	resseries.append(mkv)
	resseries.append(mp4)

#	print ()
#	print ("scan")
#	print (result)



def scanovies ():
	exit

monitor()
Have you looked at watchdog?

https://pypi.org/project/watchdog/


The reason your code doesn't work is it has no memory. You do have lists old_series and old_moves, but you never use them. You initialize them, but you don't compare the scan results to the old lists, and you don't maintain the old lists.

There is lots of information about how to do this on the internet.

https://towardsdatascience.com/implement...f8356a425d
https://thepythoncorner.com/posts/2019-0...m-changes/
https://michaelcho.me/article/using-pyth...-directory
... so many examples
I do use them

at Line 32,33 i fill the list with content in Series initially

at Line 46 i scan the Folder again and fill "resseries" with content,
then after return i compare old_series with resseries , but the compare is wrong somehow. It always returns the full content of the folder and not just the files not in list old_series.

also it jumps back from line 55 to 46 after the 10 seconds elapsed.

The lists get filled properly , checked that with a print of each list during the loop. Somehow my compare approach doesnt match
You do not use them. You doing this:
mkv= [2, 4, 6, 8]
mp4= [1, 3, 5, 7]
old_series = [mkv, mp4]

for i in range(1, 10):
    print(i in old_series)
This prints False for all numbers because old_series contains two lists [[2, 4, 6, 8]], [1, 3, 5, 7]], not numbers [1, 2, 3, 4, 5, 6, 7, 8]. You need to "flatten" your list.

To speed things up you should use sets.
import os
import sys
import time

dir_pathmovies = 'T:/latest Movies/_test/'
dir_pathseries = 'T:/Series/test/'
extensions = (".mkv", ".mp4")


def scan_files(directory, old_files):
    new_files = []
    for (dir_path, dir_names, files) in os.walk(directory):
        files = [file for file in files if file[-4:] in extensions]
        new_files += [file for file in files if file not in old_files]
        old_files.update(new_files)
    return new_files


def monitor():
    # Initialize sets of movie and series files.
    series = set()
    movies = set()
    scan_files(dir_pathmovies, movies)
    scan_files(dir_pathseries, series)

    try:
        while True:
            # Check for new series and movies
            for remaining in range(10, 0, -1):
                sys.stdout.write("\r")
                sys.stdout.write(f"{remaining:2d} seconds remaining before new DIRSCAN.")
                sys.stdout.flush()
                time.sleep(1)
            print("\n\nNew Movies:", *scan_files(dir_pathmovies, movies), sep="\n", end="\n\n")
            print("New Series:", *scan_files(dir_pathseries, series), sep="\n", end="\n\n")
    except KeyboardInterrupt:
        exit()


monitor()
And give watchdog a look.