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

  • Home
  • SEARCH
  • 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 6188265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:12:10+00:00 2026-05-24T02:12:10+00:00

I am currently writing a Python script and trying to dynamically generate some arguments.

  • 0

I am currently writing a Python script and trying to dynamically generate some arguments. However, an error is being thrown for the following script, stating 'Namespace' object is not iterable. Any ideas on how to fix?

import argparse
from os import path
import re

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

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

def main():
    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())
    args = list(set(args))
    infile.seek(0)

    parser = argparse.ArgumentParser()
    for arg in args:
        parser.add_argument('--' + arg.lower())

    replacements = vars(parser.parse_args(extra))

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

The error is with the returned value of parser.parse_known_args(). Any ideas on how I could bypass this though? Someone suggested creating an object and using the dict interface, but I don’t know what this entails exactly. I’m really new to Python, so I don’t understand why (infile, outfile), extra = parser.parse_known_args() wouldn’t work.

Edit: Updated with two fixes. First fixed the error above by using the accepted answer below. Second, also fixed an error where I was getting flagged for trying to add the same argument twice. Fixed by making args a set then back to a list. Now my script runs, but the optional arguments have no effect. Any ideas?

  • 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-24T02:12:11+00:00Added an answer on May 24, 2026 at 2:12 am

    ArgumentParser.parse_known_args returns a namespace and a list of the remaining arguments. Namespaces aren’t iterable, so when you try to assign one to the tuple (infile, outfile) you get the “not iterable” error.

    Instead, you should write something like

    namespace, extra = parser.parse_known_args()
    

    and then access the parsed arguments as namespace.infile and namespace.outfile.

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

Sidebar

Related Questions

Hey guys/gals I'm writing a python script that fixes some duplicate issues on my
I currently have Python 2.6.2 installed on my mac. I am writing a script
I am currently writing a Python script for use on Linux machines that will
I am writing a large batch-type script in Python, and need to do some
I am new to python and currently writing a module to write error logs
I'm currently writing an app in Python and need to provide localization for it.
I'm really new to C -> Python interaction and am currently writing a small
I'm currently writing a simple .sh script to parse an Exim log file for
I'm currently writing a small python app that embeds cherrypy and django using py2app.
I'm currently writing a small script for use on one of our servers using

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.