Hi I'm trying to use argparse for filename input from command line but I'm struggling to get it working.
I want to take a string passed from the command line (-d) which corresponds to a filename (datbase.csv) and store it in the variable inputargs.snp_database_location.
This gets taken as input to my load_search_snaps function as shown in my code below which opens the file and does stuff (pseudocode) to it.
import csv, sys, argparse
parser = argparse.ArgumentParser(description='Search a list of variants against the in house database')
parser.add_argument('-d', '--database',
action='store',
dest='snp_database_location',
type=str,
nargs=1,
help='File location for the in house variant database',
default='Error: Database location must be specified')
inputargs = parser.parse_args()
def load_search_snps(input_file):
with open(input_file, 'r+') as varin:
id_store_dictgroup = csv.DictReader(varin)
#do things with id_store_dictgroup
return result
load_search_snps(inputargs.snp_database_location)
using the command in bash:
python3 snp_freq_V1-0_export.py -d snpstocheck.csv
I get the following error when I try and pass it a regular csv file from the same directory using command line:
File "snp_freq_V1-0_export.py", line 33, in load_search_snps
with open(input_file, 'r+') as varin: TypeError: invalid file: ['snpstocheck.csv']
If I feed the filepath in from within the script it works perfectly. As far as I can tell I get a string for snp_database_location which matches the filename string, but then I get the error. What am I missing that's giving the type error?
nargs=1 makes inputargs.snp_database_location a list (with one element), not a string.
In [49]: import argparse
In [50]: parser = argparse.ArgumentParser()
In [51]: parser.add_argument('-d', nargs=1)
Out[51]: _StoreAction(option_strings=['-d'], dest='d', nargs=1, const=None, default=None, type=None, choices=None, help=None, metavar=None)
In [52]: args = parser.parse_args(['-d', 'snpstocheck.csv'])
In [53]: args.d
Out[53]: ['snpstocheck.csv']
To fix, remove nargs=1.
Related
I want to have some options in argparse module such as --pm-export however when I try to use it like args.pm-export I get the error that there is not attribute pm. How can I get around this issue? Is it possible to have - in command line options?
As indicated in the argparse docs:
For optional argument actions, the value of dest is normally inferred from the option strings. ArgumentParser generates the value of dest by taking the first long option string and stripping away the initial -- string. Any internal - characters will be converted to _ characters to make sure the string is a valid attribute name
So you should be using args.pm_export.
Unfortunately, dash-to-underscore replacement doesn't work for positional arguments (not prefixed by --).
E.g:
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('logs-dir',
help='Directory with .log and .log.gz files')
parser.add_argument('results-csv', type=argparse.FileType('w'),
default=sys.stdout,
help='Output .csv filename')
args = parser.parse_args()
print args
# gives
# Namespace(logs-dir='./', results-csv=<open file 'lool.csv', mode 'w' at 0x9020650>)
So, you should use 1'st argument to add_argument() as attribute name and metavar kwarg to set how it should look in help:
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('logs_dir', metavar='logs-dir',
nargs=1,
help='Directory with .log and .log.gz files')
parser.add_argument('results_csv', metavar='results-csv',
nargs=1,
type=argparse.FileType('w'),
default=sys.stdout,
help='Output .csv filename')
args = parser.parse_args()
print args
# gives
# Namespace(logs_dir=['./'], results_csv=[<open file 'lool.csv', mode 'w' at 0xb71385f8>])
Dashes are converted to underscores:
import argparse
pa = argparse.ArgumentParser()
pa.add_argument('--foo-bar')
args = pa.parse_args(['--foo-bar', '24'])
print args # Namespace(foo_bar='24')
Concise and explicit but probably not always acceptable way would be to use vars():
#!/usr/bin/env python3
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('a-b')
args = vars(parser.parse_args())
print(args['a-b'])
getattr(args, 'positional-arg')
This is another OK workaround for positional arguments:
#!/usr/bin/env python3
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('a-b')
args = parser.parse_args(['123'])
assert getattr(args, 'a-b') == '123'
Tested on Python 3.8.2.
I guess the last option is to change shorten option -a to --a
import argparse
parser = argparse.ArgumentParser(description="Help")
parser.add_argument("--a", "--argument-option", metavar="", help="") # change here
args = parser.parse_args()
option = args.a # And here
print(option)
def main(argv):
parser = argparse.ArgumentParser()
parser.add_argument('p', 'projectType', type=str, help = 'c or c++ project type', choices=['c', 'c++'])
parser.add_argument('i', 'inputfile', type=pathlib.Path, help = 'the input file path')
parser.add_argument('o',"outputfile",type=pathlib.Path, help= 'the output file path')
parser.add_argument
args = parser.parse_args()
when i run this code on command prompt, and enter the inputfile first for example.. it gives me an error. does Argparse have an option like getopt (where i call something with a letter before inputting it so no mix up ex, '-i ...input...'). Here this option is just for optional arguments.
Is there a way for positional arguments to do that?
As per the argparse documentation you would define it as an "optional" argument but with the required flag set to true, e.g.:
parser.add_argument('-i', '--inputfile', required=True, type=pathlib.Path, help='the input file path')
I'm trying to write a command line tool for Python that I can run like this..
orgtoanki 'b' 'aj.org' --delimiter="~" --fields="front,back"
Here's the script:
#!/usr/bin/env python3
import sys
import argparse
from orgtoanki.api import create_package
parser = argparse.ArgumentParser()
parser.add_argument('--fields', '-f', help="fields, separated by commas", type=str, default='front,back')
parser.add_argument('--delimiter', '-d', help="delimiter", type= str, default='*')
args = parser.parse_args()
name=sys.argv[1]
org_src=sys.argv[2]
create_package(name, org_src, args.fields, agrs.delimiter)
When I run it, I get the following error:
usage: orgtoanki [-h] [--fields FIELDS] [--delimiter DELIMITER]
orgtoanki: error: unrecognized arguments: b aj.org
Why aren't 'b' and 'ab.org' being interpreted as sys.argv[1] and sys.argv[2], respectively?
And will the default work as I expect it to, if fields and delimiter aren't supplied to the command line?
The error here is caused by argparse parser which fails to apprehend the 'b' 'aj.org' part of the command, and your code never reaches the lines with sys.argv. Try adding those arguments to the argparse and avoid using both argparse and sys.argv simultaneously:
parser = argparse.ArgumentParser()
# these two lines
parser.add_argument('name', type=str)
parser.add_argument('org_src', type=str)
parser.add_argument('--fields', '-f', help="fields, separated by commas",
type=str, default='front,back')
parser.add_argument('--delimiter', '-d', help="delimiter",
type= str, default='*')
args = parser.parse_args()
You then can access their values at args.name and args.org_src respectively.
The default input to parser.parse_args is sys.argv[1:].
usage: orgtoanki [-h] [--fields FIELDS] [--delimiter DELIMITER]
orgtoanki: error: unrecognized arguments: b aj.org
The error message was printed by argparse, followed by an sys exit.
The message means that it found strings in sys.argv[1:] that it wasn't programmed to recognize. You only told it about the '--fields' and '--delimiter' flags.
You could add two positional fields as suggested by others.
Or you could use
[args, extras] = parser.parse_known_args()
name, org_src = extras
extras should then be a list ['b', 'aj.org'], the unrecognized arguments, which you could assign to your 2 variables.
Parsers don't (usually) consume and modify sys.argv. So several parsers (argparse or other) can read the same sys.argv. But for that to work they have to be forgiving about strings they don't need or recognize.
I would like my script to receive these mutually exclusive input options:
an input file containing a JSON (script.py -i input.json);
a string containing a JSON (script.py '{"a":1}');
a JSON from stdin (echo '{"a":1}' | script.py or cat input.json | script.py).
and these mutually exclusive output options:
an output file containing a JSON;
a JSON in stdout.
So I tried with this code
import json,sys,argparse
parser = argparse.ArgumentParser(description='Template for python script managing JSON as input/output format')
group = parser.add_mutually_exclusive_group()
group.add_argument('--input-file', '-i', type=str, help='Input file name containing a valid JSON.', default=sys.stdin)
group.add_argument('json', nargs='?', type=str, help='Input string containing a valid JSON.' , default=sys.stdin)
parser.add_argument('--output-file', '-o',type=str, help='Output file name.')
args = parser.parse_args()
if not sys.stdin.isatty():
data = sys.stdin.read()
else:
# args = parser.parse_args()
if args.input_file :
data=open(args.input_file).read()
elif args.json :
data=args.json
datain=json.loads(data)
dataout=json.dumps(datain, indent=2)
if args.output_file :
output_file=open(args.output_file, 'w')
output_file.write(dataout+'\n')
output_file.close()
else:
print (dataout)
But it does not work with stdin as it requires at least one of the two group options.
How can I add stdin in the list of input options?
Adding the default=sys.stdin argument works if I call it like that
echo '{}' | ./script.py -
but not like that:
echo '{}' | ./script.py
I would take advantage of argparse.FileType with a default value of sys.stdin.
import json,sys,argparse
parser = argparse.ArgumentParser(description='Template for python script managing JSON as input/output format')
group = parser.add_mutually_exclusive_group()
group.add_argument(
'--input-file', '-i',
type=argparse.FileType('r'),
default=sys.stdin,
help='Input file name containing a valid JSON.')
group.add_argument(
'json',
nargs='?',
type=str,
help='Input string containing a valid JSON.')
parser.add_argument(
'--output-file', '-o',
type=argparse.FileType('w'),
help='Output file name.',
default=sys.stdout)
args = parser.parse_args()
data = args.json or args.input_file.read()
datain=json.loads(data)
dataout=json.dumps(datain, indent=2)
args.output_file.write(dataout)
With:
group.add_argument('--input-file', '-i')
You could test
if args.input_file is None:
<-i wasn't supplied>
else:
if args.input_file == '-':
f = sys.stdin
else:
f = open(args.input_file)
data = f.read() # etc
Or may be better:
if args.input_file == '-':
data = sys.stdin.read()
else
with open(args.input_file) as f:
f.read()
A tricky thing with stdin is that you don't want to close it after use like you would with a regular file name. And you can't use it in a with.
Similarly with stdout.
Some code sets a flag when it opens a file, as opposed to receiving an already open one, so it can remember to close the file at the end.
group.add_argument('--input-file','-i',nargs='?', default=None, const=sys.stdin)
would set arg.input_file to stdin when given -i without an argument. But I think looking for a plain - string is a better idea.
This is probably a silly question, but I have a python script that current takes in a bunch of arguments using argparser and I would like to load this script as a module in another python script, which is fine. But I am not sure how to call the module as no function is defined; can I still call it the same way I do if I was just invoking it from cmd?
Here is the child script:
import argparse as ap
from subprocess import Popen, PIPE
parser = ap.ArgumentParser(
description='Gathers parameters.')
parser.add_argument('-f', metavar='--file', type=ap.FileType('r'), action='store', dest='file',
required=True, help='Path to json parameter file')
parser.add_argument('-t', metavar='--type', type=str, action='store', dest='type',
required=True, help='Type of parameter file.')
parser.add_argument('-g', metavar='--group', type=str, action='store', dest='group',
required=False, help='Group to apply parameters to')
# Gather the provided arguments as an array.
args = parser.parse_args()
... Do stuff in the script
and here is the parent script that I want to invoke the child script from; it also uses arg parser and does some other logic
from configuration import parameterscript as paramscript
# Can I do something like this?
paramscript('parameters/test.params.json', test)
Inside the configuration directory, I also created an init.py file that is empty.
The first argument to parse_args is a list of arguments. By default it's None which means use sys.argv. So you can arrange your script like this:
import argparse as ap
def main(raw_args=None):
parser = ap.ArgumentParser(
description='Gathers parameters.')
parser.add_argument('-f', metavar='--file', type=ap.FileType('r'), action='store', dest='file',
required=True, help='Path to json parameter file')
parser.add_argument('-t', metavar='--type', type=str, action='store', dest='type',
required=True, help='Type of parameter file.')
parser.add_argument('-g', metavar='--group', type=str, action='store', dest='group',
required=False, help='Group to apply parameters to')
# Gather the provided arguments as an array.
args = parser.parse_args(raw_args)
print(vars(args))
# Run with command line arguments precisely when called directly
# (rather than when imported)
if __name__ == '__main__':
main()
And then elsewhere:
from first_module import main
main(['-f', '/etc/hosts', '-t', 'json'])
Output:
{'group': None, 'file': <_io.TextIOWrapper name='/etc/hosts' mode='r' encoding='UTF-8'>, 'type': 'json'}
There may be a simpler and more pythonic way to do this, but here is one possibility using the subprocess module:
Example:
child_script.py
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-n", "--name", help="your name")
args = parser.parse_args()
print("hello there {}").format(args.name)
Then another Python script can call that script like so:
calling_script.py:
import subprocess
# using Popen may suit better here depending on how you want to deal
# with the output of the child_script.
subprocess.call(["python", "child_script.py", "-n", "Donny"])
Executing the above script would give the following output:
"hello there Donny"
One of the option is to call it as subprocess call like below:
import subprocess
childproc = subprocess.Popen('python childscript.py -file yourjsonfile')
op, oe = childproc.communicate()
print op