Run command line containing multiple strings from python script

2024/10/14 12:20:31

Hello i am trying to autogenerate a PDF, i have made a python script that generates the wanted PDF but to generate it i have to call my_cover.py -s "Atsumi" -t "GE1.5s" -co "Japan" from my command line.

Does anyone know an easy way to call this command line from within my python script. In the script i will prompt the user to input the 3 strings which currently are "Atsumi", "GE1.5s" and "Japan" but these should change with whatever the user inputs and should therefore also change in the command line call. Any help is much appreciated

site_name = raw_input('Name of wind turbine site: ')
turbine_name = raw_input('Name of turbine type: ')
country_name = raw_input('Name of country location: ')parser = argparse.ArgumentParser()
parser.add_argument('-s', '--site')
parser.add_argument('-t', '--turbine')
parser.add_argument('-c', '--country') args = parser.parse_args()with open('cover.tex','w') as f:f.write(content%args.__dict__)cmd = ['pdflatex', '-interaction', 'nonstopmode', 'cover.tex']
proc = subprocess.Popen(cmd)
proc.communicate()retcode = proc.returncode
if not retcode == 0:os.unlink('cover.pdf')raise ValueError('Error {} executing command: {}'.format(retcode, ' '.join(cmd))) os.unlink('cover.tex')
os.unlink('cover.log')"Code to run the command line goes here"
'''my_cover.py -s "Atsumi" -t "GE1.5s" -co "Japan"'''
Answer

docopt is a great way to do program interfaces, but it would encourage syntax like cover.py --country=Japan --site=Atsumi --turbine=GE15.s (if you want flags).

In my experience docopt makes you rethink your program ideology, what it does and how to achive this. For example, you can experiment with calls like

cover.py cover.pdf --country=Japan --site=Atsumi --turbine=GE15.s
cover.py Japan Atsumi GE15.s

It seems your pdf gerenation is taken care of, but I'd also suggest:

  • get rid on raw_input(), if you can - you are having the command line args anyways
  • split script to functions that do one thing, eg preparing a tex file and writing a pdf file
  • take a use of if __name__ == '__main__':
https://en.xdnf.cn/q/117955.html

Related Q&A

Identify value across multiple columns in a dataframe that contain string from a list in python

I have a dataframe with multiple columns containing phrases. What I would like to do is identify the column (per row observation) that contains a string that exists within a pre-made list of words. Wi…

ipython like interpreter for ruby

I come from python background and am learning ruby. IPython is really awesome. I am new to ruby now, and wanted to have some sort of ipython things. As of now am having tough time, going along ruby lin…

Django dynamic verification form

Im trying to create a verification form in Django that presents a user with a list of choices, only one of which is valid.For example, for a user whose favourite pizza toppings includes pineapple and r…

Any method to denote object assignment?

Ive been studying magic methods in Python, and have been wondering if theres a way to outline the specific action of:a = MyClass(*params).method()versus:MyClass(*params).method()In the sense that, perh…

Delete lines found in file with many lines

I have an Excel file (.xls) with many lines (1008), and Im looking for lines that have anything with 2010. For example, there is a line that contains 01/06/2010, so this line would be deleted, leaving …

Combining semaphore and time limiting in python-trio with asks http request

Im trying to use Python in an async manner in order to speed up my requests to a server. The server has a slow response time (often several seconds, but also sometimes faster than a second), but works …

Import of SWIG python module fails with apache

Importing a python mdule throws an exception in django when I run with apache. The same source code works fine with the django development server. I can also import the module from the command line. Th…

Pro-Football-Reference Team Stats XPath

I am using the scrapy shell on this page Pittsburgh Steelers at New England Patriots - September 10th, 2015 to pull individual team stats. For example, I want to pull total yards for the away team (46…

How to delete the last item of a collection in mongodb

I made a program with python and mongodb to do some diaries. Like thisSometimes I want to delete the last sentence, just by typing "delete!" But I dont know how to delete in a samrt way. I do…

Python+kivy+SQLite: How to set label initial value and how to update label text?

everyone,I want to use kivy+Python to display items from a db file. To this purpose I have asked a question before: Python+kivy+SQLite: How to use them together The App in the link contains one screen.…