I like to give my user's an example in my __doc__
usage string. Here are the important parts of the code relating to my question.
# jmetadata.py
'''
usage: jmetadata.py inDirPath outFilePath
example: jmetadata.py "\\\\Hal\\hal free agent 1\\backups\\videos\\" out.txt
'''
<...deleted code...>
def forPyWinTests():
jmetadata("\\\\Hal\\hal free agent 1\\backups\\videos\\", "out.txt")
NUM_ARGS = 2
def main():
args = sys.argv[1:]
print args
if len(args) != NUM_ARGS or "-h" in args or "--help" in args:
print __doc__
sys.exit(2)
jmetadata(args[0], args[1])
if __name__ == '__main__':
main()
# print()
# forPyWinTests()
I EXPECT this behaviour:
>jmetadata.py
[]
usage: jmetadata.py inDirPath outFilePath
example: jmetadata.py "\\Hal\hal free agent 1\backups\videos\" out.txt
>
However, when I copy the example to the command line I EXPECT the program to RUN. Instead I get:
>jmetadata.py "\\Hal\hal free agent 1\backups\videos\" out.txt
['\\\\Hal\\hal free agent 1\\backups\\videos" out.txt']
usage: jmetadata.py inDirPath outFilePath
example: jmetadata.py "\\Hal\hal free agent 1\backups\videos\" out.txt
Note the SECOND LINE in the above. That results from the print args
command inside main()
. It is saying that the args list has only 1 element, ['\\\\Hal\\hal free agent 1\\backups\\videos" out.txt']
, instead of 2. This doesn't make sense to me.
I have done a bit more investigation by writing a windows .cmd file. Here is show_args.cmd:
rem show_args.cmd
echo %1
echo %2
Here is the result of a run:
>show_args.cmd "\\Hal\hal free agent 1\backups\videos\" out.txt
>rem show_args.cmd
>echo "\\Hal\hal free agent 1\backups\videos\"
"\\Hal\hal free agent 1\backups\videos\"
>echo out.txt
out.txt
Windows seems to separate the items. I am stumped. Does anyone know what's goin on here?
I hope someone has some insight to share. Thanks in advance.
Try removing the trailing backslash. Change this:
jmetadata.py "\\Hal\hal free agent 1\backups\videos\" out.txt
to this:
jmetadata.py "\\Hal\hal free agent 1\backups\videos" out.txt
In your test run above, the arguments seen by Python are:
['\\\\Hal\\hal free agent 1\\backups\\videos" out.txt']
The trailing backspace is being interpreted as an escape character since it is immediately preceding a "
, which turns your arguments into a single string (or until an unescaped "
is encountered).