Python OSError: [Errno 2] 没有这样的文件或目录

1 投票
2 回答
19860 浏览
提问于 2025-04-15 17:10

我在Linux终端上写这个脚本时,收到了一个错误信息:“OSError: [Errno 2] 没有这样的文件或目录”。有人能帮我吗,谢谢!

#!/home/build/test/Python-2.6.4

import os, subprocess

   # Create a long command line
cmd =[\
 "si createsandbox --yes --hostname=be", \
 " --port=70", \
 " --user=gh", \
 " --password=34", \
 "  --populate --project=e:/project.pj", \
 " --lineTerminator=lf new_sandbox"\
 ]

outFile = os.path.join(os.curdir, "output.log")
outptr = file(outFile, "w")

errFile = os.path.join(os.curdir, "error.log")
errptr = file(errFile, "w")

retval = subprocess.call(cmd, 0, None, None, outptr, errptr)

errptr.close()
outptr.close()

if not retval == 0:
  errptr = file(errFile, "r")
  errData = errptr.read()
  errptr.close()
  raise Exception("Error executing command: " + repr(errData))

2 个回答

0

试着这样修改:

cmd =[\
  "si", \
  " createsandbox --yes --hostname=be", \
  " --port=70", \
  " --user=gh", \
  " --password=34", \
  "  --populate --project=e:/project.pj", \
  " --lineTerminator=lf new_sandbox"\
]

我猜 subprocess.call 会把双引号中的第一个参数当作一个命令来处理。

5

如果你的脚本里有错误,可能是在这一行出的问题

errptr = file(errFile, "r")

你可以这样做

if os.path.exists(errFile):
  errptr = file(errFile, "r")
  errData = errptr.read()
  errptr.close()
  raise Exception("Error executing command: " + repr(errData))

另外,试试在命令“si”前加上完整路径,比如用 /usr/bin/si,而不是单纯的 si

撰写回答