Passing arguments into os.system - python

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'])

Related

Input a text file into a program

I'm working on a PDF generator project. The goal is to have a program that takes document files and generate a PDF file. I'm having trouble in finding a way to input a file into the program to be converted.
I started out by using the input function, where I input the file in the terminal. As a test, I wanted to input, open, read, and print a csv file containing US zipcode data. The rest of the program opens, reads and prints out some of the data. Here is the code:
import csv
file = input("Drop file here: ")
with open(file, 'r', encoding='utf8') as zf:
rf = csv.reader(zf, delimiter=',')
header = next(rf)
data = [row for row in rf]
print(header)
print(data[1])
print(data[10])
print(data[100])
print(data[1000])
When I opened the terminal to input the file this error (TypeError: 'encoding' is an invalid keyword argument for this function) appeared.
Is there a better way I can code a program to input a file so it can be open and converted into a PDF?
There are more things going on and as was mentioned in the comments, in this case it is very relevant which version of python are you using. A bit more of the back story.
input built-in has different meaning in Python2 (https://docs.python.org/2.7/library/functions.html#input) or Python3 (https://docs.python.org/3.6/library/functions.html#input). In Python2 it reads the user input and tries to execute it as python code, which is unlikely what you actually wanted.
Then as pointed out, open arguments are different as well (https://docs.python.org/2.7/library/functions.html#open and https://docs.python.org/3.6/library/functions.html#open).
In short, as suggested by #idlehands, if you have both version installed try calling python3 instead of python and this code should actually run.
Recommendation: I would suggest not to use interactive input like this at all (unless there is a good reason to do that) and instead let the desired filename be passed in from outside. I'd opt for argparse (https://docs.python.org/3.6/library/argparse.html#module-argparse) in this case which very comfortably gives you great flexibility, for instance myscript.py:
#!/usr/bin/env python3
import argparse
import sys
parser = argparse.ArgumentParser(description='My script to do stuff.')
parser.add_argument('-o', '--output', metavar='OUTFILE', dest='out_file',
type=argparse.FileType('w'), default=sys.stdout,
help='Resulting file.')
parser.add_argument('in_file', metavar='INFILE', nargs="?",
type=argparse.FileType('r'), default=sys.stdin,
help='File to be processed.')
args = parser.parse_args()
args.out_file.write(args.in_file.read()) # replace with actual action
This gives you the ability to run the script as a pass through pipe stuff in and out, work on specified file(s) as well as explicitly use - to denote stdin/stdout are to be used. argparse also gives you command line usage/help for free.
You may want the specifics tweak for different behavior, but bottom line, I'd still go with a command line argument.
EDIT: I should add more more comment for consideration. I'd write the actual code (a function or more complex object) performing the wanted action so that it exposes ins/outs through its interfaces and write the command line to gather these bits and call my action code with it. That way you can reuse it from another Python script easily or write a GUI for that should you need/want to.

What's the Python analogue of fileinput.input() for output?

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.

Read from stdin or input file with argparse

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")

replace loading a file to a list with command line/shell version in python

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.

Remove parsed options and their values from sys.argv

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.

Categories

Resources