Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法将参数从批处理文件传递到python文件_Python_Windows_Batch File_Python 2.x - Fatal编程技术网

无法将参数从批处理文件传递到python文件

无法将参数从批处理文件传递到python文件,python,windows,batch-file,python-2.x,Python,Windows,Batch File,Python 2.x,我正在尝试将参数从批处理文件传递到python文件。 我遵循了这两个链接中给出的步骤: 下面是python文件的一部分,我试图在其中传递参数: def main(argv): imapServ = 'imap.gmail.com' filename = 'TestRunLog.log' attachment = open("{} {}".format(argv[0], filename), 'rb') ....##rest of the code import sys try: if

我正在尝试将参数从批处理文件传递到python文件。
我遵循了这两个链接中给出的步骤:

下面是python文件的一部分,我试图在其中传递参数:

def main(argv):
imapServ = 'imap.gmail.com'
filename = 'TestRunLog.log'
attachment = open("{} {}".format(argv[0], filename), 'rb')
....##rest of the code

import sys
try:
if __name__ == '__main__':
   print 'go ahead'
   main(sys.argv[:1])
except ImportError:
   print 'hi'
另外,这里是批处理文件的一部分,我使用它将参数发送到Python文件:

c:\python27\python.exe C:\Users\abcd\Documents\automation\testsendemail.py %%myhome%\Documents\automation\Testresults\%resultDir%
pause
上面,
%resultDir%
是基于时间戳生成的变量

以下是输出:

go ahead
Traceback (most recent call last):
C:/Users/abcd/Documents/automation/testsendemail.py\TestRunLog.log
  File "C:/Users/abcd/Documents/automation/testsendemail.py", line 44, in <module>
    main(sys.argv[:1])
  File "C:/Users/abcd/Documents/automation/testsendemail.py", line 25, in main
    attachment = open("{} {}".format(argv[0], filename), 'rb')
IOError: [Errno 2] No such file or directory: 'C:/Users/abcd/Documents/automation/testsendemail.py TestRunLog.log'
继续
回溯(最近一次呼叫最后一次):
C:/Users/abcd/Documents/automation/testsendmail.py\TestRunLog.log
文件“C:/Users/abcd/Documents/automation/testsendmail.py”,第44行,在
主(系统argv[:1])
文件“C:/Users/abcd/Documents/automation/testsendemail.py”,第25行,主目录
附件=打开(“{}{}”。格式(argv[0],文件名),'rb')
IOError:[Errno 2]没有这样的文件或目录:“C:/Users/abcd/Documents/automation/testsendmail.py TestRunLog.log”

我关注了很多关于这个问题的问题,但是我仍然无法运行。不知道错误在哪里

这个问题与python如何与argv一起工作有关。 在此场景中,当您运行时:

main(sys.argv[:1]) # (["C:\Users\abcd\Documents\automation\testsendemail.py"])
实际上,您只获得传递给python脚本的第一个参数,即当前脚本位置

要获取除第一个参数外的所有参数,必须修复该数组筛选器:

main(sys.argv[1:]) # ["%%myhome%\Documents\automation\Testresults\%resultDir%"])
请注意,第二个过滤器还将包括您可能添加到命令行的任何其他参数


另外,作为旁注。您应该考虑使用STD LIB来加入路径。 应该是这样的:

from os.path import join
(...)
filename = 'TestRunLog.log'
attachment = open(join(argv[0], filename), 'rb')

这个问题与python如何与argv一起工作有关。 在此场景中,当您运行时:

main(sys.argv[:1]) # (["C:\Users\abcd\Documents\automation\testsendemail.py"])
实际上,您只获得传递给python脚本的第一个参数,即当前脚本位置

要获取除第一个参数外的所有参数,必须修复该数组筛选器:

main(sys.argv[1:]) # ["%%myhome%\Documents\automation\Testresults\%resultDir%"])
请注意,第二个过滤器还将包括您可能添加到命令行的任何其他参数


另外,作为旁注。您应该考虑使用STD LIB来加入路径。 应该是这样的:

from os.path import join
(...)
filename = 'TestRunLog.log'
attachment = open(join(argv[0], filename), 'rb')