Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6156605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:38:22+00:00 2026-05-23T20:38:22+00:00

Currently, I have a script which does the following. If I have text file

  • 0

Currently, I have a script which does the following. If I have text file with the lines:

<<Name>> is at <<Location>>.
<<Name>> is feeling <<Emotion>>.

The script will take in the this input file as a command line argument and prompt the user for the variables:

Name? Bob
Location? work
Emotion? frustrated

Note that name is only asked once. The script also takes in an output file as an argument and will place in the file the following.

Bob is at work.
Bob is feeling frustrated.

Now I am trying to extend the script so that I can input the variables from the command line (as if I already knew what it was going to ask). So the command would be like (in this case):

python script.py infile outfile Bob work frustrated

And it would generate the same output. Ideally, the extension should prompt the user for remaining variables if there are more remaining after those put in the command line. So if I run the script as:

python script.py infile outfile Bob work

The script would still prompt:

Emotion?

Excess variables in the command line would be ignored. I am pretty new to Python, so while this seems pretty simple, I haven’t had success updating my current script with this add-on. Attached is the script:

import argparse
from os import path
import re

replacements = {}
pattern = '<<([^>]*)>>'

def user_replace(match):
    ## Pull from replacements dict or prompt
    placeholder = match.group(1)
    if placeholder in replacements:
        return replacements[placeholder]
    ## .setdefault(key, value) returns the value if present, else sets it then returns
    return replacements.setdefault(placeholder, raw_input('%s? ' % placeholder))

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('infile', type=argparse.FileType('r'))
    parser.add_argument('outfile', type=argparse.FileType('w'))
    args = parser.parse_args()

    matcher = re.compile(pattern)

    for line in args.infile:
        new_line = matcher.sub(user_replace, line)
        args.outfile.write(new_line)

    args.infile.close()
    args.outfile.close()

if __name__ == '__main__':
    main()

Edit: The input file example above is arbitrary. In actuality, the input file could have any number of variables, repeated any number of times.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T20:38:23+00:00Added an answer on May 23, 2026 at 8:38 pm

    OK, if you want to dynamically generate the options:

    parser = argparse.ArgumentParser()
    parser.add_argument('infile', type=argparse.FileType('r'))
    parser.add_argument('outfile', type=argparse.FileType('w'))
    
    required, extra = parser.parse_known_args()
    infile, outfile = required.infile, required.outfile
    
    args = re.findall(pattern, infile.read())
    infile.seek(0)
    
    parser = argparse.ArgumentParser()
    for arg in args:
        parser.add_argument('--' + arg.lower())
    
    replacements = vars(parser.parse_args(extra))
    

    This will give you a dictionary of all the arguments. Each argument value will be a string inside a list. Then you just do

    def user_replace(match):
        """Pull from replacements dict or prompt"""
        placeholder = match.group(1)
        return (replacements[placeholder][0] 
                 if placeholder in replacements else 
                  raw_input('%s? ' % placeholder))
    

    Edit: I’ve edited the code at the top to set the arguments. This way, the arguments are optional, and you can have any number. Their names will still be name, location, and emotion in replacements.

    The user can now do:

    python script.py infile outfile --name Bob --emotion 'extremely frustrated'
    

    leaving out the ones they want to be prompted for, and enclosing strings with spaces in quotes.

    Edit 2: Edited the top part so it dynamically gets the args from the textfile.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a shell script which copies a few files to the current directory,
I have a SQL script that inserts data (via INSERT statements currently numbering in
I'm currently experimenting with build script, and since I have an ASP.net Web Part
I have a bash script that creates a Subversion patch file for the current
Friends, I have written the following JavaScript to ascertain which row of an tabular
I have the following Code which I know doesn't work correctly. Yes I know
I have a Nant script which updates a directory with the latest source code
I have a perl script that will perform some operations on directories, and I
I have some mod_rewrite rewrites that correctly redirect the URLs to a script. From
I currently have an MS Access application that connects to a PostgreSQL database via

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.