I am trying to use optparse (to parse command line options to my script) and fileinput (to have the flexibility to provide data input via pipe or file).
import optparse, fileinput
parser = OptionParser()
parser.add_option("-v", action="store_true", dest="verbose")
(options, args) = parser.parse_args()
for line in fileinput.input:
process(line)
However fileinput tries to use the '-v' option as well as file name resulting in 'No such file or directory error'. So either I need to make fileinput args or remove the parsed options from sys.argv, however I don't know a elegant way of doing it. Any pointers?
From the documentation:
To specify an alternative list of filenames, pass it as the first argument to input(). A single file name is also allowed.
So you can just pass in the leftover args you get from optparse.
Related
fileinput.input() allows to simply loop over all lines in either a list of input files provided via sys.argv[1:] or sys.stdin if the former is empty.
Is there a similarly simple way to output to the last argument if given and sys.stdout otherwise?
You can use the argparse module and add a commandline argument like this:
parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'),
default=sys.stdout)
You can check if the final argument is a file, in which case it is an input so set the output to sys.stdout, otherwise open a new file with that name as output and remove it from sys.argv.
Alternatively just use sys.stdout and let your users use > filename to store to a file.
Does argparse support a pattern such as:
foo.py {-f list_of_filenames.txt|file [file ...]}
I achieve this at the moment with the following argparse definition:
parser = argparse.ArgumentParser()
parser.add_argument("--file", "-f")
parser.add_argument("files", nargs="*")
and performing the mutual exclusivity + required check in my code rather than letting argparse do the check.
Can argparse handle a pattern of this sort?
argparse takes fromfile-prefix-chars parameter
https://docs.python.org/3.4/library/argparse.html#fromfile-prefix-chars
So if the filenames are in a file, one name per line
>>> parser = argparse.ArgumentParser(fromfile_prefix_chars='#')
>>> parser.add_argument('files', nargs='*')
>>> parser.parse_args(['#filenames.txt'])
Will read the names from the file. It puts them in the sys.argv list just as though you had entered them on the command line.
I'd like to use argparse to read from either stdin or an input file. In other words:
If an input file is given, read that.
If not, read from stdin only if it's not the terminal. (i.e. a file is being piped in)
If neither of these criteria are satisfied, signal to argparse that the inputs aren't correct.
I'm asking for behavior similar to what's described in this question, but I want argparse to recognize no file as a failed input.
Using the information from the question you linked to, what about using sys.stdin.isatty() to check if the instance your program is being run is part of a pipeline, if not, read from input file, otherwise read from stdin. If the input file does not exist or stdin is empty throw an error.
Hope that helped.
I would recommend just settings nargs='?' and then handling the case of a Nonetype separately. According to the official documentation, "FileType objects understand the pseudo-argument '-' and automatically convert this into sys.stdin for readable FileType objects and sys.stdout for writable FileType objects". So just give it a dash if you want stdin.
Example
import argparse
import sys
parser = argparse.ArgumentParser()
parser.add_argument('inputfile', nargs='?', type=argparse.FileType('r'))
if not inputfile:
sys.exit("Please provide an input file, or pipe it via stdin")
I need to execute the following command through python. rtl2gds is a tool which reads in 2 parameters: Path to a file and a module name
rtl2gds -rtl=/home/users/name/file.v -rtl_top=module_name -syn
I am reading in the path to the file and module name from the user through argparse as shown below:
parser = argparse.ArgumentParser(description='Read in a file..')
parser.add_argument('fileread', type=argparse.FileType('r'), help='Enter the file path')
parser.add_argument('-e', help='Enter the module name', dest='module_name')
args = parser.parse_args()
os.system("rtl2gds -rtl=args.fileread -rtl_top=args.module_name -syn")
But the file path that is read into args.fileread does not get in to the os.system when I call -rtl=args.fileread. Instead, args.fileread itself is being assumed as the file name and the tool flags an error.
I am sure there is a way to read in command line arguments into os.system or some other function (may be subprocess?- but couldnt figure out how). Any help is appreciated.
Don't use os.system(); subprocess is definitely the way to go.
Your problem though is that you expect Python to understand that you want to interpolate args.fileread into a string. As great as Python is, it is not able to read your mind like that!
Use string formatting instead:
os.system("rtl2gds -rtl={args.fileread} -rtl_top={args.module_name} -syn".format(args=args)
If you want to pass a filename to another command, you should not use the FileType type option! You want a filename, not an open file object:
parser.add_argument('fileread', help='Enter the file path')
But do use subprocess.call() instead of os.system():
import subprocess
subprocess.call(['rtl2gds', '-rtl=' + args.fileread, '-rtl_top=' + args.module_name, '-syn'])
If rtl2gds implements command line parsing properly, the = is optional and you can use the following call instead, avoiding string concatenation altogether:
subprocess.call(['rtl2gds', '-rtl', args.fileread, '-rtl_top', args.module_name, '-syn'])
Hi is there a way to load a file as such, but from command line
so i can type python test.py data.txt
instead of
data = [line.strip() for line in open("/home/user/data.txt", "rb").readlines()]
import sys
data = [line.strip() for line in open(sys.argv[1], "rb")]
No need for .readlines(), by the way.
To pass a parameter to your script from the command line you would want to utilize sys.argv or fileinput.
sys.argv
The list of command line arguments passed to a Python script. argv[0]
is the script name (it is operating system dependent whether this is a
full pathname or not). If the command was executed using the -c
command line option to the interpreter, argv[0] is set to the string
'-c'. If no script name was passed to the Python interpreter, argv[0]
is the empty string.
fileinput:
import fileinput
for line in fileinput.input():
process(line)
This iterates over the lines of all files listed in sys.argv[1:],
defaulting to sys.stdin if the list is empty. If a filename is '-', it
is also replaced by sys.stdin. To specify an alternative list of
filenames, pass it as the first argument to input(). A single file
name is also allowed.
Have a look at the argparse module. It is fairly easy to use and even allows you to directly supply and open files from the command line arguments.