Home > OS >  How create a function for defining .pdf size
How create a function for defining .pdf size

Time:03-31

Let's take the case I should print the number of bytes taken by PDF files in the directory tree (".pdf" extensions). I wrote down the following code which seems to work but, I am not sure it returns what I am looking for.

import os
def find_pdf_size(files):
    for root, dirs, files in os.walk(top):
        for name in files:
            if name.endswith(".pdf"):
                print(os.path.getsize(top)) 
  
top = 'C:/Users/PC/Desktop/Preludium/Preludium(2)/Articles/MM'
print(find_pdf_size(top))

Since this code returns always the same number, repeated for the number of files within the file folder, I suppose. Could you please let me know how to move out of this?

CodePudding user response:

Your code

import os
def find_pdf_size(files):
    for root, dirs, files in os.walk(top):
        for name in files:
            if name.endswith(".pdf"):
                print(os.path.getsize(top)) 
  
top = 'C:/Users/PC/Desktop/Preludium/Preludium(2)/Articles/MM'
print(find_pdf_size(top))

does report size of top which does not change so you get repeated value use os.path.join to get path to file you want to measure, that is do

import os
def find_pdf_size(files):
    for root, dirs, files in os.walk(top):
        for name in files:
            if name.endswith(".pdf"):
                print(os.path.getsize(os.path.join(root,name))) 
  
top = 'C:/Users/PC/Desktop/Preludium/Preludium(2)/Articles/MM'
print(find_pdf_size(top))

From os.walk docs

Note that the names in the lists contain no path components. To get a full path (which begins with top) to a file or directory in dirpath, do os.path.join(dirpath, name).

CodePudding user response:

Try this.

import os


def sizeof_fmt(num, suffix="B"):
    for unit in ["", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"]:
        if abs(num) < 1024.0:
            return f"{num:3.1f}{unit}{suffix}"
        num /= 1024.0
    return f"{num:.1f}Yi{suffix}"


def find_pdf_size(path):
    for root, dirs, files in os.walk(path):
        for name in files:
            if name.endswith(".pdf"):
                print(f"File name: {name} and its size: {sizeof_fmt(os.path.getsize(name))}")


path = 'C:/Users/PC/Desktop/Preludium/Preludium(2)/Articles/MM'
print(find_pdf_size(path))

Or without format

import os

def find_pdf_size(path):
    for root, dirs, files in os.walk(path):
        for name in files:
            if name.endswith(".pdf"):
                print(f"File name: {name} and its size: {(os.path.getsize(name))}")


path = 'C:/Users/PC/Desktop/Preludium/Preludium(2)/Articles/MM'
print(find_pdf_size(path))

CodePudding user response:

You did a small mistake while passing argument in your function get_size_of. Update your code with the function below

import os
    path = r"C:\Users\HPvns\Desktop\cycle"
    def print_size(path):
        for root, directories, files in os.walk(path, topdown=False):
            for name in files:
                if name.endswith(".pdf"):
                    print(os.path.getsize(os.path.join(root, name)))


  • Related