This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author 888xray999
Recipients 888xray999
Date 2020-03-04.08:40:21
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1583311221.81.0.793172357388.issue39845@roundup.psfhosted.org>
In-reply-to
Content
I have this code for a tool that copies files from one directory to another:

parser = argparse.ArgumentParser()
parser.add_argument('src_dir')
parser.add_argument('dest_dir')
args = parser.parse_args()

It works fine on Unix, but on Windows, if the destination path ends with a backward slash, it seems that argparse parses the string as it would escape a double quote and returns the string with the double quote appended.

For example, calling the script:
(base) PS Z:\test> python.exe .\main.py -d Z:\tmp\test\DJI\ 'C:\unu doi\'
will create the destination path string: C:\unu doi"
The source path, even though it ends with the backslash as well, isn't modified by argparse.

I've worked around this issue by using this validation function for the arguments:

def is_valid_dir_path(string):
    """
    Checks if the path is a valid path

    :param string: The path that needs to be validated
    :return: The validated path
    """
    if sys.platform.startswith('win') and string.endswith('"'):
        string = string[:-1]
    if os.path.isdir(string):
        return string
    else:
        raise NotADirectoryError(string)

parser = argparse.ArgumentParser()
parser.add_argument('src_dir', type=is_valid_dir_path)
parser.add_argument('dest_dir', type=is_valid_dir_path)
args = parser.parse_args()
History
Date User Action Args
2020-03-04 08:40:21888xray999setrecipients: + 888xray999
2020-03-04 08:40:21888xray999setmessageid: <1583311221.81.0.793172357388.issue39845@roundup.psfhosted.org>
2020-03-04 08:40:21888xray999linkissue39845 messages
2020-03-04 08:40:21888xray999create