RE: [Python] File Sorter 06-19-2014, 02:59 PM
#10
(06-19-2014, 01:39 PM)Anima Templi Wrote: You want feedback? Well, take a look at mine, lol. Maybe add a config file like I did, to easily add new file types/folders. Also, as already suggested, make it take arguments from the command line. This will make it much easier for the user, to actually choose a path.
(06-19-2014, 12:19 PM)L0aD1nG Wrote: You could grab the whole code to a function and do this..
Code:import optparse import sys def my_sweet_sort(path): <the whole code> def main(): parser = optparse.OptionParser("Usage : " + sys.argv[0] + " -p <path you would like sort>" ) parser.add_option('-p', dest = 'sort_path', type = 'string', help = 'This is the path location you want to sort') (options, args) = parser.parse_args() sort_path = options.sort_path if not sort_path: print parser.usage exit(1) else: my_sweet_sort(sort_path) exit(0) ## At last check if debug mode given and run it if __name__ == '__main__': main()
So you can now do this from the cmd:
python myscript.py -p <path to sort>
I just made a suggestion i hope that is allowed
Well done posting the code so everyone can learn from i love people like you!
Also well done doing that with python!!!
Cheers!
I really won't recommend this code, sorry.
Optparse has been deprecated since of Python 2.7 besides, argparse has several more features than optparse.
- handling positional arguments
- supporting sub-commands
- allowing alternative option prefixes like + and /
- handling zero-or-more and one-or-more style arguments
- producing more informative usage messages
- providing a much simpler interface for custom types and actions
Second, it's not really recommended to create a main function like that in Python. Absolutely no need for it. And "my_sweet_sort() ? Function/class and variable names should tell as much about the function/value they hold.
I hope you don't take this the wrong way, but since you gave feedback, I thought it was ok for me to do the same thing.
First of all i havent tried that argparse module but i think that same can happen with optparse..but i am not sure as i havent check the argparse module yet.
On the other hand now i didnt recommend him naming functions like that i just wanted to show him the way that optparse works so i just putted a random and little funny name to the function.Also using the main funtion is highly recommended by pro pythonistaz cause its a way to nicely organize all the code at last and run it like that after check if not debuging flag is on.
MASTERING OTHERS IS STRENGTH, MASTERING YOURSELF IS TRUE POWER.
![[Image: qJweLN6.jpg]](http://i.imgur.com/qJweLN6.jpg)



![[+]](https://sinister.li/images/modern/collapse_collapsed.png)