0

I have a list of images.

Lst = ["1.jpeg", "5.jpeg", "8.jpeg", ....]

I want to know their individual size in Kb/MB. I am using

du - bsh <filename>

But The folder structure is:

Folder/Subfolder1/1.jpeg, 10.jpeg, ... 
Folder/Subfolder2/11.jpeg, 190.jpeg, ...
Folder/Subfolder1/5.jpeg, 90.jpeg, ...
...
Jan Wilamowski
  • 3,308
  • 2
  • 10
  • 23
Techython
  • 1
  • 1
  • What's the problem here? – ForceBru Feb 21 '22 at 12:07
  • Does this answer your question? [How can I check file size in Python?](https://stackoverflow.com/questions/2104080/how-can-i-check-file-size-in-python) – Jan Wilamowski Feb 21 '22 at 12:08
  • If you are open to doing this in bash check out the ```tree``` command. You might need to install it from your package manager. but ``` man tree``` should give you some ideas. It shows you sub folders and aloows you to filter within them (say limit it to only JPEG files). – Anant Feb 21 '22 at 12:15
  • in Python you can use `glob.glob('*/1.jpeg', recursive=True)` to search this name in all structure. If you start in `Folder` then it will search only in its subfolders (and subsubfolders, etc.). Eventually you can use `'*.jpeg'` to get all images in all subfolders - `glob.glob('*/*.jpeg', recursive=True)` and later check which ends with `1.jpeg`. And when you will have pathes then you can use `os.stat(path).st_size` to get size – furas Feb 21 '22 at 18:25
  • in Python you can use `glob.glob('**/1.jpeg', recursive=True)` to search this name in all structure. You can even use `'Folder/**/1.jpeg'` to search only in `Folder`. Eventually you can use `'*.jpeg'` to get all images in all subfolders - `glob.glob('**/*.jpeg', recursive=True)` and later check which ends with `1.jpeg`, `5.jpeg`, etc. And when you will have pathes then you can use `os.stat(path).st_size` to get size. – furas Feb 21 '22 at 18:34
  • Do you mean Kb, kB or kiB? In any case, it's not clear where your problem is. I guess the nudge in the right direction is to split the task, one is to locate the files, the other to retrieve the size of one and then finally the combination of both. In any case, please read the description of tags you applied, both "shell" and "linux" are rather bad choices. – Ulrich Eckhardt Feb 21 '22 at 18:50

1 Answers1

1

In Python you can get file size using stat()

os.stat(path).st_size

And you can find file(s) using glob.glob with *, ? or **.

files = glob.glob("Folder/*/1.jpeg")

for path in files:
    print(path, os.stat(path).st_size)

It always gives list with all matching paths - even if found only one file or it didn't find any file.

If it may have file in Folder/sub1/sub2/sub3/1.jpeg then you can use ** and recursion

files = glob.glob("Folder/**/1.jpeg", recursive=True)

Of course you can use string formating (.format() or f-string) to search different files from list

for name in Lst:
    files = glob.glob(f"Folder/**/{name}", recursive=True)

Or you can use *.jpeg to get all files and later check if it on the list

files = glob.glob("Folder/**/*.jpeg", recursive=True)

for path in files:
    filename = path.split('/')[-1]
    if filename in Lst:
        print(path, os.stat(path).st_size)        

EDIT:

In Bash you can also use *

for path in Folder/*/*.jpeg 
do 
   du -bsh $path 
done

or in one line

for path in Folder/*/*.jpeg; do du -bsh $path; done

but it still needs to filter files which are on list.

But maybe it could be simpler to use { }

for path in Folder/*/{1,5,8,10,11}.jpeg

For more help in Bash you could ask on similar portals: Unix & Linux or Ask Ubuntu

furas
  • 134,197
  • 12
  • 106
  • 148