subprocess出错。 "系统找不到指定的文件"

时间:2016-01-14 10:17:07

标签: python oracle subprocess sqlplus

这是我的代码:

import urllib
import requests

from bs4 import *
from subprocess import Popen,PIPE
import os

connectString = 'SYSTEM/mediadot123'

def runSqlQuery(sqlCommand, connectString):
   session = Popen(['sqlplus', '-S', connectString], stdin=PIPE, stdout=PIPE, stderr=PIPE)
   session.stdin.write(sqlCommand)
   return session.communicate()

session = Popen(['sqlplus','-S','hr/hr'], stdin=PIPE, stdout=PIPE, stderr=PIPE)
stdout, stderr = session.communicate()

sqlCommand = "insert into food(title, recipe, image) values ('bla','bla','bla');"
queryResult, errorMessage = runSqlQuery(sqlCommand, connectString)
print queryResult

它给了我以下错误:

C:\Python27\python.exe C:/Users/Umer/PycharmProjects/DATACRAWLER/main.py

Traceback (most recent call last):

  File "C:/Users/Umer/PycharmProjects/DATACRAWLER/main.py", line 38, in <module>

session = subprocess.Popen(['sqlplus','-S','hr/hr'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

  File "C:\Python27\lib\subprocess.py", line 710, in __init__
    errread, errwrite)

  File "C:\Python27\lib\subprocess.py", line 958, in _execute_child
    startupinfo)

WindowsError: [Error 2] The system cannot find the file specified

1 个答案:

答案 0 :(得分:3)

考虑使用绝对路径执行命令 某些二进制文件不在PATH中,具体取决于您的用户,系统和软件安装。

要找出sqlplus所在的位置,请在cmd.exewhere sqlplus中运行以下内容,这样可以为您提供绝对路径。

然后只需:

Popen(['C:/path/sqlplus.exe', '-S', ...])

另外,为了找出PATH环境变量中的实际内容,您可以执行以下操作:

print(os.environ['PATH'])