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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:38:33+00:00 2026-06-09T20:38:33+00:00

This simplified script is enough to cause the issue… just checking if the ‘-d’

  • 0

This simplified script is enough to cause the issue… just checking if the ‘-d’ argument is a valid directory, supplying a default if it’s not provided…

#!/usr/bin/python

import os
import argparse

def valid(dir):
   subdir = dir + '/Desktop'
   if not os.path.exists(subdir):
      raise argparse.ArgumentTypeError("%s is not a valid directory" % subdir)
   return dir

parser = argparse.ArgumentParser(description="blah blah blah")
parser.add_argument('-d', '--directory', help='directory to check', default=os.getcwd(), type=valid)
args = parser.parse_args()

And it doesn’t matter what the default argument is, when I run the script it uses the default, no matter what I enter on the command line, and throws an uncaught exception as follows:

Traceback (most recent call last):
  File "./parsertest.py", line 15, in <module>
    args = parser.parse_args()
  File "/usr/lib/python2.7/argparse.py", line 1688, in parse_args
    args, argv = self.parse_known_args(args, namespace)
  File "/usr/lib/python2.7/argparse.py", line 1710, in parse_known_args
    default = self._get_value(action, default)
  File "/usr/lib/python2.7/argparse.py", line 2239, in _get_value
    raise ArgumentError(action, msg)
argparse.ArgumentError: argument -d/--directory: /home/users/jrice/Desktop/Desktop is not a valid directory

Runs fine, and by fine I mean, handles the ArgumentTypeError as and when it should, just printing the msg when if I do the following:

  1. Remove the ‘default=’ argument
  2. Do not append ‘/Desktop’ to dir, so subdir = dir, or just check dir itself
  3. Run the script from my home directory!?!?

Elaboration: If I do any of the above, even if ‘-d’ isn’t valid, everything is fine. This is the output, which is what I want.

>./Desktop/parsertest.py -d blah
usage: parsertest.py [-h] [-d DIRECTORY]
parsertest.py: error: argument -d/--directory: blah/Desktop is not a valid directory

why should os.getcwd() + ‘/Desktop’ be any different?

  • 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-09T20:38:35+00:00Added an answer on June 9, 2026 at 8:38 pm

    Argparse attempts to convert the default argument to whatever type was given to it.

    import argparse
    
    parser = argparse.ArgumentParser(description="blah blah blah")
    parser.add_argument('-i',default="1",type=int)
    
    args = parser.parse_args([])
    print args          # Namespace(i=1)
    print type(args.i)  # <type 'int'>
    

    The reason for this design choice is a little weird to me, but it is probably so that you can pass strings to default just as it would get them on the commandline and then the help will be formatted properly.

    Note I don’t really like passing validation code to the type keyword argument even though they do it in the documentation. That argument is to convert the input string into some other type. If you really want to do the validation as you parse, you should consider using a custom Action, but for this example, it’s probably just easiest to do:

    #...snip...
    parser.add_argument('-d', '--directory', help='directory to check')
    args = parser.parse_args()
    args.directory = valid(args.directory if args.directory is not None else os.getcwd())
    #the following should work too. 
    #args.directory = valid(args.directory if args.directory else os.getcwd()) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just created an acc on SO to ask this :) Assuming this simplified example:
I'm debugging a perl script which looks like this (simplified): #!/usr/bin/perl use strict; use
im trying to solve this issue. I have a web site with this simplified
i have this (simplified) <script> dataToSend = 'dummy data here'; function sendIt() { //code
This is a simplified version of what I want to accomplish: In my script
This my simplified script: Sub SomeOtherSub(Stattyp As String) 'Daty and the other variables are
My JS: <script type=text/javascript> function DictionaryEntry() { var self = this; self.Simplified = ko.observable();
Having this (simplified) XML: <?xml version=1.0 encoding=UTF-8?> <kml> <Document> <Placemark> <name>Poly 1</name> <Polygon> <coordinates>
Given this simplified snippet : <html> <body> <div> <div style='text-align:center;'>asdfaskjdfakjsd</div> <div style='float:right'> <input type='submit'
Imagine this simplified markup: <div id=header> <!-- Other things.... --> <div id=detail> </div> </div>

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.