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 8486413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:56:22+00:00 2026-06-10T20:56:22+00:00

My script currently uses sys.argv to check for an input file provided to the

  • 0

My script currently uses sys.argv to check for an input file provided to the program.

I am trying to utilise argparse instead but I cant seem to get it to work. I was able to set it up and add an argument, but when I parse an argument, and print that parsed argument, I get a namespace. How can I get a string? Basically, I want to take the argument as a string, and open a file with that name.

Currently, my sys.argv is:

filename = sys.argv[1]
f = open(filename, 'r')

My argparse prints out a Namespace as follows:

arg = parser.parse_args()
print arg

How can I use that to open a file? I want to use argparse since the error handlign for arguments there is a lot easier.

  • 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-06-10T20:56:24+00:00Added an answer on June 10, 2026 at 8:56 pm

    think its preferable (or something!) to use the with statement to open the file like this:

    # printfile.py
    import argparse
    
    parser = argparse.ArgumentParser(description="Opens a file and does cool stuff ^^")
    parser.add_argument('filename', type=str, help="Path to file to open")
    args = parser.parse_args()
    
    with open(args.filename) as f:
        print '   my uber cool file:'
        print f.readlines()
    

    specifying those keyword args also helps make a pretty -h help text option (which is neat neat)

    [dlam@dlam-63221:~] $ python printfile.py -h
    usage: printfile.py [-h] filename
    
    Opens a file and does cool stuff ^^
    
    positional arguments:
        filename    Path to file to open
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a script, which uses file globbing via the sys.argv variable like
I am currently trying to write a script that uses expect to logon to
I currently have a script that scrapes proxies off websites, but I'm just wondering
Currently all of my script's settings are located in a PHP file which I
I have a php script that sends an email. It currently uses the php
I am working with a consulting group on a program which currently uses a
I'm currently working on an application that uses py2exe to make an exe file
My Problem: I have an R script myscript.R that uses a configuration file, e.g.
In the process of trying to write a Python script that uses PIL today,
I have an R script that plots data from a file. The script currently

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.