我正在尝试使用Python来自动化涉及调用Fortran可执行文件并提交一些用户输入的过程.我花了几个小时阅读类似的问题和尝试不同的事情,但没有运气.这是一个显示我上次尝试的最小例子
#!/usr/bin/python
import subprocess
# Calling executable
ps = subprocess.Popen('fortranExecutable',shell=True,stdin=subprocess.PIPE)
ps.communicate('argument 1')
ps.communicate('argument 2')
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试运行它时,我收到以下错误:
File "gridGen.py", line 216, in <module>
ps.communicate(outputName)
File "/opt/apps/python/epd/7.2.2/lib/python2.7/subprocess.py", line 737, in communicate
self.stdin.write(input)
ValueError: I/O operation on closed file
Run Code Online (Sandbox Code Playgroud)
非常感谢任何建议或指示.
编辑:
当我调用Fortran可执行文件时,它会询问用户输入如下:
fortranExecutable
Enter name of input file: 'this is where I want to put argument 1'
Enter name of output file: 'this is where I want to put argument 2'
Run Code Online (Sandbox Code Playgroud)
不知何故,我需要运行可执行文件,等待它询问用户输入然后提供该输入.
我想在我的python代码和SQL数据库之间建立连接.我已经阅读了几种方法,但我没有得到结果.
conn = pymysql.connect(user=X,passwd=X,host=X,port=X,database=X, charset='utf8', autocommit=True)
curs = conn.cursor()
try:
curs.execute('SELECT id,sing_name,bir_yr FROM singers_list WHERE bir_yr = ? ',year)
data = curs.fetchall()
for i in data:
yield " Data: " + str(i) + "<br/>"
except:
yield " Failed to get data from base<br/>"
Run Code Online (Sandbox Code Playgroud)
其中year是一个int python变量.我得到了正确的结果:
curs.execute('SELECT id,sing_name,bir_yr FROM singers_list)
Run Code Online (Sandbox Code Playgroud)
这意味着我成功连接到数据库.如何在查询中包含变量?(不仅是整数,还有字符串或任何类型)
我有一个QThread派生类,通过向它发送QEvents与主线程进行通信.
主线程与第二个线程通信的最佳方法是什么?
主线程有一个指向第二个的指针.
我正在使用蟒蛇。我正在尝试运行一个进程并使用 subprocess.popen 获取输出。在阅读之后,我看到人们使用了communication()[0] 但这不在文档中,它在一个示例中使用。我想知道是否有人可以告诉我为什么要附加 [0] 到communication()?
import subprocess
import sys
proc = subprocess.Popen(["program.exe"], stdin=subprocess.PIPE) #the cmd program opens
proc.communicate(input="filename.txt") #here the filename should be entered (runs)
#then the program asks to enter a number:
proc.communicate(input="1") #(the cmd stops here and nothing is passed)
proc.communicate(input="2") # (same not passing anything)
Run Code Online (Sandbox Code Playgroud)
我如何使用python传递和与cmd通信.
谢谢.(使用Windows平台)
我正在使用 Gphoto2 在 DSLR 上拍照。由于它基于我尝试使用的 bash 命令,subprocess.communicate但在相机拍照后它会冻结。
如果我gphoto2 --capture-image-and-download在终端中尝试它需要不到 2 秒。我正在研究树莓派。
代码:
import subprocess
class Wrapper(object):
def __init__(self, subprocess):
self._subprocess = subprocess
def call(self,cmd):
p = self._subprocess.Popen(cmd, shell=True, stdout=self._subprocess.PIPE, stderr=self._subprocess.PIPE)
out, err = p.communicate()
return p.returncode, out.rstrip(), err.rstrip()
class Gphoto(Wrapper):
def __init__(self, subprocess):
Wrapper.__init__(self,subprocess)
self._CMD = 'gphoto2'
def captureImageAndDownload(self):
code, out, err = self.call(self._CMD + " --capture-image-and-download")
if code != 0:
raise Exception(err)
filename = None
for line in out.split('\n'):
if line.startswith('Saving file as '):
filename …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用python的子进程模块运行php脚本.
proc = subprocess.Popen(['php', '-f', test.php], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
retCode = proc.wait
print retCode
val = float(kprocess.stdout.read())
return val
Run Code Online (Sandbox Code Playgroud)
我也尝试过:
proc = subprocess.Popen(['php', '-f', test.php], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
val = float(kprocess.communicate()[0])
return val
Run Code Online (Sandbox Code Playgroud)
当我在python解释器中运行它时,两种方式都在本地工作,但是当我尝试在实际服务器上运行它时,我总是得到"ValueError at/empty string for float()".这让我相信这个过程在某种程度上没有被等待.我错过了什么?
编辑:我正在使用Django,所以当我使用Django运行时它似乎只会破坏.
communicate ×7
python ×6
subprocess ×5
cmd ×1
mysql ×1
popen ×1
qt ×1
raspberry-pi ×1
stdin ×1
variables ×1
wait ×1
windows ×1