Executing Multi-Line Python Statements in a Single Command-Line Command
The Python -c option allows for one-liner loop execution, but importing modules within the command can lead to syntax errors. To address this, consider the following solutions:
<code class="bash">echo -e "import sys\nfor r in range(10): print 'rob'" | python</code>
<code class="bash">python -c "exec(\"import sys\nfor r in range(10): print 'rob'\")"</code>
<code class="bash">(echo "import sys" ; echo "for r in range(10): print 'rob'" ) | python</code>
As suggested by SilentGhost and Crast (not shown in this response).
These solutions allow for the execution of multi-line Python statements within a single command-line command, while also importing necessary modules.
The above is the detailed content of How to Execute Multi-Line Python Statements in a Single Command-Line Command?. For more information, please follow other related articles on the PHP Chinese website!