How do I pass command line arguments to a path-ind

2019-08-08 20:51发布

问题:

I'm using Python to write various command-line scripts to process seismic data. The data that is processed is in many different directories, representing data from many different seismometers. I need to have some path independence for the scripts so that I don't have to always copy, cut, and paste the scripts into the directory containing the data. To that end, I've tried to set the path in the system environment in order to make the scripts runnable from anywhere. Tis is the method I chose to use.

I set the system path to include the following:

 ";C:/Anaconda/Python.exe;C:/Anaconda/sigscript/"

I then added to the PATHEXT the following

";.py"

I then made sure that the .py extension was associated to python.exe so that if discovered, the operating system will pass it to python.exe

Now, whenever I am at the command line prompt, regardless of the current working directory, if I have a python script in directory sigscript, it launches and runs. My problem however, is that it runs, but when it's passed to the python.exe it strips off all command line arguments!

using the script myscript.py,

import os,sys

for i in range(0,len(sys.argv)):
    print "Argument {0} = '{1}'".format(i,sys.argv[i])

If this script is run by explicitly sending it to Python, I get the following output:

c:\Anaconda\sigscript\> Python myscript.py arg1 arg2 arg3
Argument 0 = 'myscript.py'
Argument 1 = 'arg1'
Argument 2 = 'arg2'
Argument 3 = 'arg3'

...which is as it should be! Arguments are being passed into the code.

However, if I try to run the code and let the operating system find and associate the script to python from outside of this directory (or even from inside the directory), I get this:

c:\Seismic\> myscript.py arg1 arg2 arg3
Argument 0 = 'C:\Anaconda\sigscript\myscript.py'

Thus the remaining arguments are stripped out.

Evidently, setting up the script to execute the way I did has some problems. However I need to centralize my code somehow, and not be forced into explicitly typing out the source directory name. How can I move through the directories, and execute my scripts while preserving the ability to pass the command line arguments?

FWIW, Here is my current PATH environmental variable:

PATH=C:\ProgramData\Oracle\Java\javapath;
C:\Program Files\Common Files\Microsoft Shared\Windows Live;
C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live;
C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;
C:\Windows\system32;
C:\Windows;
C:\Windows\System32\Wbem;
C:\Windows\System32\WindowsPowerShell\v1.0\;
C:\Program Files\Intel\DMIX;C:\Program Files (x86)\Simply Fortran\mingw-w64\bin;
C:\Program Files (x86)\Common Files\GTK\2.0\bin;
C:\Program Files (x86)\MATLAB\R2012a Student\bin;
C:\Program Files (x86)\Git\cmd;
C:\Anaconda;
C:\Anaconda\Scripts;
C:\Program Files (x86)\Windows Live\Shared;
C:\Program Files (x86)\QuickTime\QTSystem\;
C:\Dwimperl\perl\bin;C:\Dwimperl\perl\site\bin;
C:\Dwimperl\c\bin;c:\gmt5\bin;C:\Program Files (x86)\Skype\Phone\;
C:\Anaconda\sigscript\;
C:\Program Files\Agilent\IO Libraries Suite\bin;
C:\Program Files\IVI Foundation\VISA\Win64\agvisa;
C:\Program Files\IVI Foundation\VISA\Win64\bin;
C:\Program Files (x86)\Agilent\IO Libraries Suite\bin;
C:\Program Files (x86)\IVI Foundation\VISA\WinNT\agvisa;
C:\Program Files (x86)\IVI Foundation\VISA\WinNT\bin;
C:\Program Files (x86)\IVI Foundation\IVI\bin;
C:\Program Files\IVI Foundation\IVI\bin;
C:\Program Files (x86)\IVI Foundation\VISA\WinNT\Bin\;
C:\Program Files\IVI Foundation\VISA\Win64\Bin\;
C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;
C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.0\;
C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;
C:\Anadonca\python.exe