r/learnpython • u/pltnk • May 31 '19
My first Python project: command line application that copies all files with given extension from a directory and its subfolders to another directory showing progress bar and remaining files counter.
This project was inspired by the task from Chapter 9 of Automate the Boring Stuff but I tried to push it a little bit further by adding a possibility of preservation source folder structure, log creation, progress bar, remaining files counter and command line interface.
I am new to programming and GitHub, so any feedback either on the code itself or design of README.md and so on is highly appreciated!
340
Upvotes
6
u/fernly May 31 '19
I agree with other comments about tidy and readable. Some general comments, not necessarily applicable for a small private project, but consider for something you might distribute.
I don't think the error checking is complete. to_folder could be nested inside from_folder or vice versa, which might cause problems. However checking for this presents an interesting coding challenge. (hmmm... split one path on os.sep, put all those names in a set...)
I don't like the idea of collecting log lines and then writing them in a batch at the end. (I guess at the end, the source seems to be truncated so I don't see the actual calls to e.g. copy_with_structure() or save_log().) Better to have log writes embedded at the point of action; that way you don't lose all the logging if the program crashes in the middle of the run.
You could easily add a log-level param to argparse with a default of ERROR, and write individual file-copy log lines with INFO priority so they don't clutter the log unless wanted.
I think your progress bar code will be something you'll use again; suggest moving it to its own file and importing it.
Consider how best to license your code, and add a license statement in the docstring. Creative Commons or GPL or whatever.
Now we are getting far into the weeds. You essentially duplicate the file scan between get_total() and copy(). If you ever want to redesign, consider how to capture selected filenames and paths one time while counting them, forming a to-do list of some kind, and then do the copy from that to-do list instead of calling os.walk() a second time. Record the to-do actions in such a way that you only need one copy function, not two.