Oct-28-2020, 05:55 PM
(This post was last modified: Oct-28-2020, 06:24 PM by snippsat.
Edit Reason: Added code tag
)
I'm new to python and stuck on the concept of command-line arguments.
The variable filename and input filename must both refer to files that exist and can be read by the
program. The output filename must refer to a file that can be opened for writing by the program.
The following operations are performed.
First, the variable file is read and each line is interpreted as the definition of a variable. The text
before = on a line is the name of the variable, and the text after = is its value. Collectively, the data
read in this step are called the templating variables. The variable file is closed. Here is the code that I need to modify to use sys.argv[1] as the variable file, sys.argv[2] as the input file, and sys.argv[3] as the output file.
The variable filename and input filename must both refer to files that exist and can be read by the
program. The output filename must refer to a file that can be opened for writing by the program.
The following operations are performed.
First, the variable file is read and each line is interpreted as the definition of a variable. The text
before = on a line is the name of the variable, and the text after = is its value. Collectively, the data
read in this step are called the templating variables. The variable file is closed. Here is the code that I need to modify to use sys.argv[1] as the variable file, sys.argv[2] as the input file, and sys.argv[3] as the output file.
import sys import varsub if len(sys.argv) != 3: print("Usage: {} INFILE OUTFILE".format(sys.argv[0])) print("The text file INFILE is then converted to an HTML file OUTFILE.") sys.exit() infn = sys.argv[1] outfn =sys.argv[2] # open input and output files fin = open(infn,"r") fout = open(outfn,"w")