I am very new to python and stack overflow. In a directory, I have a list of many sub directories with many files within each. I am trying to figure out a way to find the largest file within each sub directory, and copy (move) these files to a new directory. Is this possible, any help is much appreciated.
Asked
Active
Viewed 763 times
-2
-
5The question you're posing is pretty open-ended, as the answer to "is this possible" is generally "yes". Is there a specific part of this that you're having trouble with? – Makoto May 04 '15 at 04:09
-
`os.walk` to [get directory listings](http://stackoverflow.com/questions/120656/directory-listing-in-python), then `os.stat` to [check file size](http://stackoverflow.com/questions/2104080/how-to-check-file-size-in-python) and `shutil` to [move](http://stackoverflow.com/questions/8858008/moving-a-file-in-python)/[copy](http://stackoverflow.com/questions/123198/how-do-i-copy-a-file-in-python) file. – Paul Rooney May 04 '15 at 04:16
-
1You've included the `shutil` tag in your question. I assume that means you've already found that module and its docs, which implies you know how to do at least part of this. So, show us what you _do_ know how to do, with something like `size = ??? (pathname) ### here's where I'm stuck` where you don't know what to do. – abarnert May 04 '15 at 04:16
1 Answers
2
You'll want to look at the os module, and the os.path submodule in that. os.listdir
and os.path.getsize
will both be useful. You can use os.rename
to move a file, or, for maximum compatibility, can use the shutil
module's shutil.move
, which will help if you're moving things from one filesystem to another.

cge
- 9,552
- 3
- 32
- 51