I'd like to do something like:
% python foo bar
import sys
sys.argv
to get:
% ['foo', 'bar']
but of course python dies when you enter an argument which is not a script or goes into non interactive mode if you do.
Can I do this somehow?
I'd like to do something like:
% python foo bar
import sys
sys.argv
to get:
% ['foo', 'bar']
but of course python dies when you enter an argument which is not a script or goes into non interactive mode if you do.
Can I do this somehow?
Use -
as script name:
python - foo bar
Test:
>>> import sys
>>> sys.argv
['-', 'foo', 'bar']
$ python - asdf asdf
Python 2.7.6 (default, Feb 15 2014, 23:06:13)
[GCC 4.8.2 20140206 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.argv
['-', 'asdf', 'asdf']
>>>
Run python this way:
python - foo bar
Then
import sys
sys.argv
will work :)
You could just set it manually for testing in IDLE:
try:
__file__
except:
sys.argv = [sys.argv[0], 'foo', 'bar']
python foo bar
won't run python –
Bedlam © 2022 - 2024 — McMap. All rights reserved.