【问题标题】:Python OSError: [Errno 2] No such file or directoryPython OSError:[Errno 2]没有这样的文件或目录
【发布时间】:2009-12-18 09:51:52
【问题描述】:

我正在尝试将此脚本写入我的 linux 终端,并且收到以下错误消息:“OSError: [Errno 2] No such file or directory”。谁能帮忙,谢谢

#!/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))

【问题讨论】:

  • 哪一行导致了这个错误?
  • 请提供实际的错误回溯和实际的错误信息。
  • 这个“si”程序是什么?它看起来很可疑,它在 Linux 脚本中使用了 Windows 文件名 E:/project.pj。它是一个 shell 脚本,还是一个可执行程序?

标签: python


【解决方案1】:

如果错误出现在您的脚本中,可能是您在这一行出现错误

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

【讨论】:

  • 在这种情况下,在尝试打开 errFile 进行写入之前不会发生错误吗?
  • "w" in file(errFile, "w") 将创建新文件或覆盖现有文件,如果文件无法写入,您应该得到写入错误或类似的东西。跨度>
  • 没错。那么如果没有发生这样的写入错误,为什么它无法按照您的建议找到文件?
  • 想起来,也许它也可能是外部“si createdandbox”命令的副作用。在任何情况下,测试文件是否存在当然是明智的,即使它不存在并不明显,但应该独立于文件的存在引发异常;)
  • 是的,可能是si createdandbox出错,甚至“si”不存在或无法运行。
【解决方案2】:

尝试像这样修改:

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

我猜测 subprocess.call 会认为 "" 中的第一个参数是命令

【讨论】:

    猜你喜欢
    • 2012-07-18
    • 1970-01-01
    • 1970-01-01
    • 2018-11-20
    • 2017-06-13
    • 1970-01-01
    • 2015-11-17
    • 1970-01-01
    • 2015-11-19
    相关资源
    最近更新 更多