To obtain command line arguments for a Python script use sys.argv
sys.argv
is a list where sys.argv[0]
is the script name
#!/usr/bin/env python import sys for argument in sys.argv: print(argument)$ ./test.py arg1 arg2 "string with spaces" ./test.py arg1 arg2 string with spaces
argparse
moduleargparse
supports short (-o
) or long (--option
) optionsimport argparse
parser = argparse.ArgumentParser(description='Best program, ever!')
parser.add_argument('-n', '--number', metavar='NUMBER', type=int,
required=True, help='Number of times to shout')
options = parser.parse_args()
print('Shout! ' * options.number)
To get user input, use raw_input()
(Python 2) or input()
(Python 3)
Python 2
name = raw_input("What is your name? ") print "Hi, %s" % name
Python 3
name = input("What is your name? ") print("Hi, %s" % name)
Result
What is your name? Avram Hi, Avram
For version-agnostic user input
Standard Library only
import sys if sys.version_info[0] > 2: INPUT = input else: INPUT = raw_input name = INPUT("What is your name? ")Using the six module
from six.moves import input name = input("What is your name? ")
open()
functionr
– read (default)w
– write, truncates filex
– open for exclusive creation, fails if existsa
– append to end of a file if it existsb
– binary mode - read and write as bytest
– text mode - read and write as strings (default)+
– read and writea+b
would open a file in binary mode without truncationIt is important to make sure file are closed when you are finished with them
try
-finally
statementfileObject = open('filename')
try:
pass # Do many fancy file things
finally:
fileObject.close()
In Python 2.5 and later, files can be opened using a with
statement
The file will automatically be closed when the with
statement completes
with open('filename') as fileObject: pass # Do many fancy file things
Open a file in text mode and read contents as a single string
>>> with open('/etc/redhat-release') as releaseFile: ... releaseFile.read() ... 'Fedora release 24 (Twenty Four)\n'
Open a file in text mode and read contents line by line
>>> with open('/etc/group') as groupFile: ... groupFile.readline() # Read the first line ... for line in groupFile: # Read the rest of the lines ... print(line.strip()) ... 'root:x:0:\n' bin:x:1: daemon:x:2: sys:x:3:
When writing to a file object, newline characters must be explicitly added
Open a file and write a series of random numbers
>>> import random >>> randomGen = random.SystemRandom() >>> with open('/tmp/random', 'w') as randomFile: ... for num in range(1, 100): ... randomNum = randomGen.randint(0, 100000) ... randomFile.write('%d\n' % randomNum) ...$ cat /tmp/random 42380 30569 43790 76564 ...
Open multiple files at once
>>> with open('/etc/group') as groupFile, open('/tmp/group', 'w') as newFile: ... for line in groupFile: ... groupname = line.split(':')[0] ... newFile.write('%s\n' % groupname) ...
When using write()
on the Python 3 console, a series of numbers will be printed to the screen.
This is the number of bytes written.
sys
module includes three special file objectssys.stdin
- Standard Insys.stdout
- Standard Outsys.stderr
- Standard Errorprint()
when Python <=2.5 must be supported>>> sys.stdout.write("Hello, from sunny standard out\n")
Hello, from sunny standard out
Write to standard error
>>> sys.stderr.write("Hello, from the cold depths of standard error\n") Hello, from the cold depths of standard error
Read from standard in
#!/usr/bin/env python import sys input = sys.stdin.readlines() input.sort() for line in input: sys.stdout.write(line.upper())$ cat /etc/group | ./capitalize.py ABRT:X:173: ADM:X:4: AUDIO:X:63: AVAHI-AUTOIPD:X:170: AVAHI:X:70: ...
A Python script will exit with a returncode of 0 if no unhandled exceptions occur
sys.exit()
None
, returncode is 0sys.exit()
sys.exit(11)
str
to standard errorif not len(sys.argv) > 1:
sys.exit("Where are your arguments?")