在windows平台上的任何位置运行Python脚本



我在windows机器(my_codes(中保留了一个文件夹,用于保存所有代码。我已将此文件夹路径添加到windows路径中的路径env变量中。

现在,当我从某个地方调用脚本时,我无法正确传递参数。

(base) C:Usersabc>test.py abc.csv
print_test
C:Usersabcmy_codestest.py
Traceback (most recent call last):
File "C:Usersabcmy_codestest.py", line 4, in <module>
print(sys.argv[1])
IndexError: list index out of range

python代码如下。我已将其保存为C:\Users\abc\my_codes 中的test.py

import sys      
print('print_test')
print(sys.argv[0])
print(sys.argv[1])

这在linux中运行得很好,但到目前为止还无法在windows中运行。

如果我这样称呼的话,这在窗户里也能用。

(base) C:Usersabc>python my_codes/test.py abc.csv
print_test
my_codes/test.py
abc.csv

但是,我不想每次都通过指定python文件的完整路径来调用。

我在powershell中创建了一个函数,如下所示:-

function test {
python C:Usersabcmy_codestest.py $args
}
(base) C:Usersabc>test abc.csv
print_test
my_codes/test.py
abc.csv

我可以称测试为abc.csv。这符合我的目的,但我很想以更合适的方式完成它。

最新更新