Home > other >  Read folders to create a dictionary of file types in python
Read folders to create a dictionary of file types in python

Time:12-26

I am new to Python, so bear with me!

I am attempting to read my folder that has thousands of .mp4 and .mov so that I can create a dictionary (associative array in php) according to their type.

Here is what I have done.

import os
import pathlib

class Meta:
    def __init__(self, path):
        self.path = path
    
    def read(self):
        path = self.path
        files = os.listdir(path)
        data = {}
        for i, file in enumerate(files):
            if not os.path.isdir(file):
                ext = pathlib.Path(file).suffix.lower().strip(".")
                # The following two lines will not work (it does in PHP)
                #data[ext][] = file 
                #data[ext][i] = file
        return data

path = "Z:\Videos"
m = Meta(path)
f = m.read()
print(f)

I were expecting to create a (dictionary) of list that looks:

   data['mp4'] = ("MP4 File 1", "MP4 File 2", "MP4 File 3")
   data['mov'] = ("MOV File 1", "MOV File 2", "MOV File 3")

Please show me the trick!

CodePudding user response:

Basically you need to just add to your dict a list for each filename, here's completed code with comments:

import os
import pathlib

class Meta:
    def __init__(self, path):
        self.path = path

    def read(self):
        path = self.path
        files = os.listdir(path)
        data = {}
        for i, file in enumerate(files):
            if not os.path.isdir(file):
                ext = pathlib.Path(file).suffix.lower().strip(".")
                filename = pathlib.Path(file).stem

                # A simple check to see if we already have data[ext]
                if ext in data:
                    # If we do, we just append it to the list
                    data[ext].append(filename)
                else:
                    # If we don't, we make new list with element inside
                    data[ext] = [filename]
        return data

path = "Z:\Videos"
m = Meta(path)
f = m.read()
print(f)

CodePudding user response:

import os
import pathlib


class Meta:
    def __init__(self, path):
        self.path = path

    def read(self):
        path = self.path
        files = os.listdir(path)

        mp4_list = []
        mov_list = []

        for file in files:

            if os.path.isdir(file):
                pass

            ext = pathlib.Path(file).suffix.lower().strip(".")

            filename = pathlib.Path(file).stem

            if ext == "mp4":
                mp4_list.append(filename)

            if ext == "mov":
                mov_list.append(filename)

        data = {
            "mp4": tuple(mp4_list),
            "mov": tuple(mov_list)
        }
        return data

path = "your-folder-path"
m = Meta(path)
f = m.read()
print(f)

As you mention in question are tuple.

data['mp4'] = ("MP4 File 1", "MP4 File 2", "MP4 File 3") data['mov'] = ("MOV File 1", "MOV File 2", "MOV File 3")

so Tuple objects implement very few methods because they are immutable (cannot be changed).

  • Related