python – OSError:[WinError87]参数不正确

前端之家收集整理的这篇文章主要介绍了python – OSError:[WinError87]参数不正确前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我有一个使用python 3.4.3编写的python脚本,它会输入一个ip地址,用户名和密码的.csv文件,以传递给另一个批处理脚本.

  1. import pdb
  2. import csv
  3. import os
  4. import subprocess
  5. import datetime
  6. import time
  7. import signal
  8. from multiprocessing import Process
  9. def callGetDimmBatchFile(logFile,batchFileName,ipAddress,userName,passWord):
  10. print('\nId: {0}'.format(counter) + '\n',file=logFile,flush=True)
  11. command ='{0} -i {1} -u {2} -p {3}'.format(batchFileName,passWord)
  12. print(command,flush=True)
  13. print("IP Address is {0}".format(ipAddress))
  14. print("User name is {0}".format(userName))
  15. print("Password is {0}".format(passWord))
  16. timeout = 60
  17. start = datetime.datetime.now()
  18. process = subprocess.Popen(command,stdout=logFile,stderr=logFile)
  19. while process.poll() is None:
  20. time.sleep(0.1)
  21. now = datetime.datetime.now()
  22. if (now - start).seconds > timeout:
  23. process.kill()
  24. # os.kill(process.pid,signal.SIGKILL)
  25. # os.waitpid(-1,os.Warning)
  26. return None
  27. rc = process.wait()
  28. print('\nReturn Code:',rc,flush=True)
  29. logFile = open('log.txt','w+')
  30. batchFileName = 'getfoo.bat'
  31. pathToCsv = 'autorun-input.csv'
  32. print('Path to CSV is {0}'.format(pathToCsv))
  33. counter = 0
  34. with open(pathToCsv) as csvFile:
  35. reader = csv.reader(csvFile,delimiter=',')
  36. for row in reader:
  37. ipAddress = row[0]
  38. userName = row[1]
  39. passWord = row[2]
  40. p = Process(target=callGetDimmBatchFile,args=(logFile,passWord))
  41. p.start()
  42. p.join()
  43. #callGetDimmBatchFile(logFile,passWord)
  44. os.system("pause")

它读入的文件(autorun-input.csv)是这样的:

  1. 10.69.69.1,taclab,taclab
  2. 10.69.69.2,taclab
  3. 10.69.69.3,taclab
  4. 10.69.69.4,taclab
  5. 10.69.69.5,taclab
  6. 10.69.69.6,taclab
  7. 10.69.69.7,taclab
  8. 10.69.69.8,taclab
  9. 10.69.69.9,taclab
  10. 10.69.69.10,taclab

它不能在几台Windows 7机器上运行,错误是这样的:

  1. C:\Users\Taclab\Desktop\DimmScript\Python-Project-9\Python-Project\DimmReport>python autorun.py
  2. Path to CSV is autorun-input.csv
  3. Traceback (most recent call last):
  4. File "autorun.py",line 44,in

我不明白哪个参数不正确.看起来p.open会抛出异常.

最佳答案
这种类型的错误通常是通过传递subprocess.Popen没有可执行文件的命令引起的.例如:

  1. subprocess.Popen(' -s') # notice the space at the beginning
  2. subprocess.Popen(['','-s']) # this will cause the same error as well

检查你的日志(你在错误之前将变量’command’写入你的日志),看看它是否由于某种原因无效

如果它没问题那么我猜它必须是日志文件,因为父进程打开它.
事实上,如果我尝试在我的电脑上做同样的事情(我在python2.7上尝试过它)它引发了一个不同的错误,关于日志文件已经关闭.

尝试做类似的事情:

  1. with open('tempLog{0}.log'.format(os.getpid(),'w+') as f:
  2. subprocess.Popen(command,stdout=f,stderr=f)

看看这是否有效

猜你在找的Python相关文章