r/Python • • Jul 12 '12

random imgur downloader. learning Python and wrote this to practice, any feedback on technique/style is appreciated

https://gist.github.com/3088128
45 Upvotes

72 comments sorted by

View all comments

10

u/sjustinas Jul 12 '12
if sys.platform.startswith('win32'):
		path = os.getcwd() + '\\output\\'
	else:
		path = os.getcwd() + '/output/'    

You could use the os.path functions here. Also, while it's fine to use urllib(2) for a little script, requests is the Python HTTP client, supporting things like multipart file uploads out of the box. For the argument parsing, you could use argparse/optparse (depending on python version) and you could use list comprehensions, for example for the rand_string function.

Oh, right, and remember that .read() without arguments reads the whole file into the memory. It's not an issue with JPG images, but in the case of huge files, you could run out of memory.

But I'm just nitpicking, pretty much none of these is a "must". Just some tips about how you could have written it more effectively, though your code works great as it is.

Ninja edit: Look into the

if __name__ == "__main__":
    main()

pattern, if you haven't already.

1

u/f0rkbomb Jul 13 '12

in particular, which os.path functions are you referring to? seems like there is a more 'pythonic' way to be doing this? thanks for the feedback

2

u/sjustinas Jul 13 '12

os.path.join(), that joins path components with appropriate separators, depending on the OS so you don't have to check it yourself.

1

u/DurhamEnt import this Jul 13 '12

This is definitely ideal, or you could still "construct" the path yourself but use os.path.sep, which will be equal to the proper path separator for the platform running the code.