[docs] Interface option "-c"

Georg Brandl georg at python.org
Thu Sep 22 10:12:55 CEST 2011


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Am 21.09.2011 15:51, schrieb Hodson, Josh:
> I tried to use the ?-c? option to run a script called ?a? with a method
> called ?Test()?, and received an error message as follows:
> 
> S:\>python -c "import a\n a.Test()" File "<string>", line 1 import a\n
> a.Test() ^ SyntaxError: unexpected character after line continuation
> character
> 
> S:\>
> 
> If I replace the ?\n? with a semicolon, then it works.  Documentation for
> the interface option ?-c? reads 
> (http://docs.python.org/using/cmdline.html#command-line):
> 
> *-c*<command>¶ 
> <http://docs.python.org/using/cmdline.html#cmdoption-unittest-discover-c>
> 
> 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.
> 
> I am running 64-bit Python27 on Windows Vista.  I have not tried this with
> any other version of Python or on any other platform.  In my personal
> opinion, the semicolon is cleaner than the ?\n? anyway, so I would prefer
> that the documentation just be updated to say ?separated by semicolons?
> instead of ?separated by newlines?, but that is assuming all other versions
> of Python on all platforms behave the same.

Hi Josh,

I think the issue here is that your command interpreter / shell does not
interpret the "\n" as a newline.  (The string that Python receives already
has to have the newline, Python itself does not translate escapes in it.)
You'd have to look into the documentation for your shell how to put
newlines in command arguments.

As for changing the suggestion to semicolons, it is not equivalent since
you cannot run code with suites (if/for/... statements) using semicolons.

cheers,
Georg
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.18 (GNU/Linux)

iEYEARECAAYFAk567gcACgkQN9GcIYhpnLCjYwCfcWfFgSth8MkBSCUAC3EwHz/K
EkQAnj3IL9OHTZI68V9iDSJsGSI8V3y2
=T3Ir
-----END PGP SIGNATURE-----


More information about the docs mailing list