How to call an external command in Python?

Five ways to call external command in Python with code examples and video 
Sergey Siminskiy's picture
Sergey Siminskiy
10 August

Hype.Codes team have found several ways to solve this issue.

1. Using subprocess module in the standard library:

from subprocess import call
call(["ls", "-l"])

The advantage of subprocess vs system is that it is more flexible.

The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function [os.system()].

2. os.system("some_command with args") passes the command and arguments to your system's shell. This is nice because you can actually run multiple commands at once in this manner and set up pipes and input/output redirection. For example:

os.system("some_command < input_file | another_command > output_file")

However, while this is convenient, you have to manually handle the escaping of shell characters such as spaces, etc. On the other hand, this also lets you run commands which are simply shell commands and not actually external programs.

3. stream = os.popen("some_command with args") will do the same thing as os.systemexcept that it gives you a file-like object that you can use to access standard input/output for that process. There are 3 other variants of popen that all handle the i/o slightly differently. If you pass everything as a string, then your command is passed to the shell; if you pass them as a list then you don't need to worry about escaping anything.

4. The call function from the subprocess module. This is basically just like the Popen class and takes all of the same arguments, but it simply waits until the command completes and gives you the return code. For example:

return_code ="echo Hello World", shell=True)  

5. If you're on Python 3.5 or later, you can use the new function, which is a lot like the above but even more flexible and returns a CompletedProcess object when the command finishes executing.

How to combine Python and Erlang?

Over view of Pyrlang Library, a Python library, that implements Erlang distribution protocol 
Sergey Siminskiy's picture
Sergey Siminskiy
21 August

Python is the most popular 2017 programming language, according to the IEEE Spectrum index. An interpreted language, Python has a design philosophy that emphasizes code readability (notably using whitespace indentation to delimit code blocks rather than curly brackets or keywords), and a syntax that allows programmers to express concepts in fewer lines of code than might be used in languages such as C++ or Java. The language provides constructs intended to enable writing clear programs on both a small and large scale.

Erlang is a general-purpose, concurrent, functional programming language, as well as a garbage-collected runtime system. Elixir, one of the most popular "young" programming language, runs on the Erlang virtual machine (BEAM).

How to combine Python and Erlang?

For this purpose, Purlang Library will suit. 

Pyrlang is a Python library which implements Erlang distribution protocol and creates an Erlang-compatible node in your Erlang cluster.

You can send and receive messages, spawn “processes” on Python side, which will be addressable from Erlang using familiar Erlang concepts like message sending to process identifiers or registered names. Also same works from Python: you can address Erlang processes, send messages to them, monitor and link with them.

The library is designed to be dropped into existing code base with very few changes required.

Learn more at official website.