How can I print all arguments passed to a python script?
This is what I was trying:
#!/usr/bin/python
print(sys.argv[1:]);
update
How can I save them to a file?
#!/usr/bin/python
import sys
print sys.argv[1:]
file = open("/tmp/test.txt", "w")
file.write(sys.argv[1:])
I get
TypeError: expected a character buffer object
To print multiple variables in Python, use the print() function. The print(*objects) is a built-in Python function that takes the *objects as multiple arguments to print each argument separated by a space.
In Python, arguments are passed to a script from the command line using the sys package. The argv member of sys ( sys. argv ) will store all the information in the command line entry and can be accessed inside the Python script. Python's getopt module can also be used to parse named arguments.
Python print() Function The print() function prints the specified message to the screen, or other standard output device. The message can be a string, or any other object, the object will be converted into a string before written to the screen.
You'll need to import sys
for that to work.
#!/usr/bin/python
import sys
print sys.argv[1:]
Example
:/tmp% cat foo.py
#!/usr/bin/python
import sys
print (sys.argv[1:]);
:/tmp% python foo.py 'hello world' arg3 arg4 arg5
['hello world', 'arg3', 'arg4', 'arg5']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With