Login Register






[Python] File Sorter filter_list
Author
Message
RE: [Python] File Sorter #7
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 Smile
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.

Reply





Messages In This Thread
[Python] File Sorter - by Ex094 - 06-19-2014, 08:24 AM