-1

I am new to the programing world and I have hit a snag on a piece of code.

What I have: I have a piece of code that identifies all .MP4 files and calculates the size of the files within a directory. So far I can only apply this code to a specific folder that I input manually. But the code works.

Problem: I would like to apply what I have to multiple folders within a directory. I have several folders with years in the file path and I would like to apply this code to each year individually. For example: I need a piece of code/direction to code that can allow me to run this code on all folders with '2021' in the name.

Any pointers or suggestions are welcome.

# import module
import os

# assign size
size = 0

# assign folder path
Folderpath = r'C:\file_path_name_here'

# get size
for path, dirs, files in os.walk(Folderpath):
    for f in files:
        if not f.endswith('.MP4'):
            continue
        else:
            fp = os.path.join(path, f)
            size += os.path.getsize(fp)

# display size
print("Folder size: " + str(size))
bad_coder
  • 11,289
  • 20
  • 44
  • 72
  • Have you tried the "glob" function. It will list all files with a common pattern. In your case, you can use glob.glob("*.MP4") – GIOVANNI QUINONES VALDEZ Nov 08 '21 at 23:53
  • Thanks for your response @GIOVANNIQUINONESVALDEZ. I don't believe that will work in the way I need it to. The end result would be for me know how the size of all .MP4 files within a given year, not just total for all years. – sungliengk Nov 09 '21 at 00:08
  • but with glob, you list all MP4 files, and then you can use the os.path.getsize function to get the size of each file individually. >> glob.glob("*.MP4") [file1.MP4, file2.MP4, ..., fileN.MP4] – GIOVANNI QUINONES VALDEZ Nov 09 '21 at 00:09
  • Thank you for your suggestion. I am getting the desired files using .rglob but when I try to determine the size of the file I get "WindowsError: [Error 2] The system cannot find the file specified: 'specificfilename.mp4'". It looks like I need to find a way to extract the full file path rather than just the name. – sungliengk Nov 10 '21 at 00:13

1 Answers1

0

You can use glob for that. If i understand you correctly you want to iterate through all Folders right? I myself am not a routined coder aswell but i use it in for a Script where i have to iterate over an unknown number of files and folders. In my case PDF's which then get scanned/indexed/merged...

This obviously returns a list of of files with which you then could workd through. os.path commonpath is also handy for that.

def getpdflisting(fpath):
    filelist = []
    for filepath in Path(os.path.join(config['Ordner']['path'] +\
            fpath)).glob('**/*.pdf'):
        filelist.append(str(filepath))
    if filelist:
        filelist = [x for x in filelist if x]
    logger.info(filelist)
    return filelist

Or even better https://stackoverflow.com/a/66042729/16573616

crpb
  • 1
  • 2
  • Thanks for your response crpb. Just so to check that I am understanding the concept I can use .glob to gather all the folders with 20xx year in the name and then plug that into the # assign folder path Folderpath = r'C:\file_path_name_here' element? – sungliengk Nov 09 '21 at 00:05
  • ok, i thought they would be sorted ..take a look here for matching the 20\d{2} - https://stackoverflow.com/a/51246151/16573616 – crpb Nov 09 '21 at 00:15