Python Forum
Monitoring a Directory for new mkv and mp4 Files
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Monitoring a Directory for new mkv and mp4 Files
#1
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()
Reply
#2
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
Reply
#3
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
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Loop through all files in a directory? Winfried 10 422 Apr-23-2024, 07:38 PM
Last Post: FortuneCoins
  uploading files from a ubuntu local directory to Minio storage container dchilambo 0 471 Dec-22-2023, 07:17 AM
Last Post: dchilambo
  change directory of save of python files akbarza 3 901 Jul-23-2023, 08:30 AM
Last Post: Gribouillis
  Using pyinstaller with .ui GUI files - No such file or directory error diver999 3 3,395 Jun-27-2023, 01:17 PM
Last Post: diver999
  Read directory listing of files and parse out the highest number? cubangt 5 2,376 Sep-28-2022, 10:15 PM
Last Post: Larz60+
  How to save files in a separate directory Scordomaniac 3 1,914 Mar-16-2022, 10:17 AM
Last Post: Gribouillis
  monitoring the temperature of the CPU with Python apollo 2 8,781 Apr-13-2021, 05:39 PM
Last Post: apollo
  IoT Air Monitoring System project_science 0 1,939 Mar-26-2021, 08:14 PM
Last Post: project_science
  Rename Multiple files in directory to remove special characters nyawadasi 9 6,436 Feb-16-2021, 09:49 PM
Last Post: BashBedlam
  List of error codes to find (and count) in all files in a directory tester_V 8 3,720 Dec-11-2020, 07:07 PM
Last Post: tester_V

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020