Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the -c flag in python

Tags:

I noticed in the python doc that there is a -c flag. Here is what python doc says:

Execute the Python code in command. command can be one or more statements separated by newlines, with significant leading whitespace as in normal module code.

There is no example in the doc and I couldn't figure out how to make this work, and also in what situations it may help.

Anyone have any clue?

like image 872
Jason Avatar asked Nov 26 '14 19:11

Jason


People also ask

When flag is used in Python?

Python Language Regular Expressions (Regex) FlagsFor some special cases we need to change the behavior of the Regular Expression, this is done using flags. Flags can be set in two ways, through the flags keyword or directly in the expression.

Where can I find the flags in Python?

In the Python source code, you can find this more extensive list of flags in code.h: Show activity on this post. These FLAGS signify different components of a code object. While the other answer points you towards the right answer, I am just adding a little fun to the mix. You can try importing a subset of FLAGS from dis.

What are optional flags in Python regex?

Python regex allows optional flags to specify when using regular expression patterns with match (), search (), and split (), among others. All RE module methods accept an optional flags argument that enables various unique features and syntax variations. For example, you want to search a word inside a string using regex.

How to use regex flags in Python to enhance search capabilities?

You can enhance this regex’s capability by adding the RE.I flag as an argument to the search method to enable case-insensitive searching. You will learn how to use all regex flags available in Python with short and clear examples. First, refer to the below table for available regex flags.

What are the different flags available in a Python executable?

Every command-line executable has some flags to configure it while it runs. Some of the popular flags include –help to get idea documentation about executable, -v to check the executable version, and -m flag to run a Python module. Although there are many other flags available, only a few of them are actually useful.


1 Answers

Just pass regular Python code as the argument to the flag:

python -c 'print 1 print 2' 

Import modules works, and blank lines are OK, too:

python -c ' import pprint pprint.pprint(1) ' 

When using this feature, just be mindful of shell quoting (and indentation), and keep in mind that if you're using this outside of a few shell scripts, you might be doing it wrong.

like image 119
Thomas Orozco Avatar answered Sep 18 '22 17:09

Thomas Orozco