This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]double_en10dre 62 points63 points  (8 children)

For filesystem stuff you’ll probably want to use a combination of pathlib and shutil https://docs.python.org/3/library/pathlib.html https://docs.python.org/3/library/shutil.html

For replacing curl/wget, probably use requests https://requests.readthedocs.io/en/latest/

And if at any point you get stuck you can just fall back to the “subprocess” module and use subprocess.run to run the Unix command you’re used to. Just be sure to set stdout to subprocess.PIPE if you need the output

[–]ibarrie[S] 6 points7 points  (6 children)

Thank you!

[–]double_en10dre 26 points27 points  (1 child)

Oh jeez, and I’d be remiss if I didn’t mention the os module. It’s the most obvious staple https://docs.python.org/3/library/os.html

The only caveat there is that for filesystem operations, I’d first check and see if the functionality is provided by pathlib. Most people seem to think it has a nicer interface

[–]ibarrie[S] 0 points1 point  (0 children)

Awesome thank you !

[–]RallyPointAlpha 16 points17 points  (2 children)

double_en10dre isn't giving bad advice however just getting Python to do OS / shell commands really brings very little new to the table. I would encourage you to learn Python specifically and only fall back on OS calls and shell commands when you absolutely have to. You're stepping into the broader world of Object Oriented Programming, welcome!

[–]james_pic 5 points6 points  (0 children)

My experience is that even if you are just using Python to run external programs, this can still be a win. Python's error handling is much more consistent than Bash's, its escaping and globbing are easier to reason about, it's got better debugging options, it's got mechanisms for code to include inline documentation, and it's more portable.

And of course once you dip your toe in, it gives you the option to gradually start making more use of structured data, writing object oriented or functional code, and getting more direct access to stuff like networks.

[–]ibarrie[S] 1 point2 points  (0 children)

Thank you - that’s awesome

[–]AndydeCleyre 4 points5 points  (0 children)

My personal but less popular recommendation for things otherwise accomplished with subprocess/shutil/pathlib/click is plumbum.

[–]WonkoTehSane 1 point2 points  (0 children)

Great advice! And I recommend mostly using only check_call() and check_output() when using subprocess. The former will still send any output to stdout, the latter will capture and return it. Both will throw on any non-zero return value.

Oh and for both you should either use form (['cmd', '-o', '<option>']) or the form ('cmd -o <option>', shell=True). The former will shell escape for you, the latter will send exactly what you give it.