Message363339
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() |
|
Date |
User |
Action |
Args |
2020-03-04 08:40:21 | 888xray999 | set | recipients:
+ 888xray999 |
2020-03-04 08:40:21 | 888xray999 | set | messageid: <1583311221.81.0.793172357388.issue39845@roundup.psfhosted.org> |
2020-03-04 08:40:21 | 888xray999 | link | issue39845 messages |
2020-03-04 08:40:21 | 888xray999 | create | |
|