Scanning directory and processing multiple files with cv2.VideoCapture()

Ok, I did a test using glob and guess what, it works:

# file operations
import glob
# Motion detection
import cv2


# Scan target directory
# os.scandir returns an iterator, make it a list to be re-usable

# Target directory is in the same directory as the script
targetDirectory = "videofiles"
mp4files = glob.glob(targetDirectory + '/*.mp4')

for element in mp4files:
    print(element)
# The above prints all found paths as videofiles\filename.mp4

for videofile in mp4files:

    print(videofile, " -- ", type(videofile))
    # This prints: videofiles\Burglary.mp4  --  <class 'str'>

    cap = cv2.VideoCapture(videofile)

    print(cap)
    # This prints: <VideoCapture 018CB4C0>

    ret, frame1 = cap.read()

    if(ret):
        heatmap = frame1.copy()  # no error
        cv2.imshow('heatmap', heatmap)
    else:
        print('error')

So it works, glob however returns relative paths.

@crackwitz: I tested the following

targetDirectory = "videofiles"
dircontent = list(os.scandir(targetDirectory))

# Search for mp4 files and make a list [ [filename, path], [filename, path], ... ]
mp4Files = [[f.name[:-4], os.path.abspath(f.name)] for f in dircontent if f.name.lower().endswith('.mp4')]

for videofile in mp4Files:

    print(videofile[1], " -- ", type(videofile[1]))
    # This prints: E:\Projects\Programming\Python\OpenCV\_CaptureSaveROI\Motiondetection_Batch_Heatmap\Burglary.mp4  --  <class 'str'>

    pathAsString = 'E:/Projects/Programming/Python/OpenCV/_CaptureSaveROI/Motiondetection_Batch_Heatmap/videofiles/Burglary.mp4'

    print(pathAsString)
    # This prints: E:/Projects/Programming/Python/OpenCV/_CaptureSaveROI/Motiondetection_Batch_Heatmap/videofiles/Burglary.mp4

    print(videofile[1] == pathAsString)
    # This prints: False

    print('types:')
    print(type(videofile[1]))       # This prints: <class 'str'>
    print(type(pathAsString))       # This prints: <class 'str'>

    cap = cv2.VideoCapture(videofile[1])

    # the following lines don't give an error:
    # cap = cv2.VideoCapture('E:/Projects/Programming/Python/OpenCV/_CaptureSaveROI/Motiondetection_Batch_Heatmap/videofiles/Burglary.mp4')
    # cap = cv2.VideoCapture(pathAsString)

    print(cap)
    # This prints: <VideoCapture 018CB4C0>

    ret, frame1 = cap.read()
    print(ret)                  # This prints: False
    heatmap = frame1.copy()     # <<<<<<< ERROR: 'NoneType' object has no attribute 'copy'

videofile[1] == pathAsString returns false because they are two different objects right? What I don’t understand is that they are both strings with the same content but when fed into cv2.VideoCapture() one works and the other doesn’t.

I don’t know if it’s relevant but I’m using pycharm on windows: