在Mac OSX上从Python 3.6运行wine命令 [英] Run wine commands from Python 3.6 on Mac OSX

查看:140
本文介绍了在Mac OSX上从Python 3.6运行wine命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用Python编写脚本,该脚本打开wine,然后将代码发送到wine终端以打开.exe程序. .exe程序也是命令驱动的.

我可以打开wine,但我无法再打开了

import shlex, subprocess

line = "/usr/bin/open -n -a /Applications/Wine\ Stable.app"
line2 = '''cd /Applications/application/ && wine application.exe /h1 
/k0 Z:/Users/myname/Desktop/File.TXT''' 
line = shlex.split(line)
p1 = subprocess.Popen(line)
p1.stdin.write(line2.encode())

上面的方法不起作用,尽管

import shlex, subprocess

line = "/usr/bin/open -n -a /Applications/Wine\ Stable.app"
line2 = '''cd /Applications/application/ && wine application.exe /h1 
/k0 Z:/Users/myname/Desktop/File.TXT''' 
line = shlex.split(line)
p1 = subprocess.Popen(line)
p1.stdin.write(line2.encode())

似乎没有接收到第2行

/usr/bin/open -n -a /Applications/Wine\ Stable.app

本身就很好(它打开Wine,但没有其他功能.)

我对下一步应该做什么感到很困惑.我希望尽可能避免其他依赖项,因为它看起来很简单.

解决方案

在许多情况下(在Linux上),以下这些对我有用:

import subprocess

command = 'echo "echo foo && echo bar" | wine cmd > std_out.txt 2> std_error.txt &'
subprocess.Popen(command, shell = True)

(我相信wine也可以在MacOS上作为命令使用.如果我错了,请纠正我.)

该命令启动类似Windows/DOS的外壳程序(wine cmd).您实际上可以在Linux shell中键入wine cmd并按Enter键-您将在DOS shell中找到自己.下一步是使命令进入DOS shell.我通过将它们以字符串形式传送到其中来完成此操作.在我的示例中,我运行两个命令:echo fooecho bar.最初的echo将命令字符串写入stdout,随后的|打开一个管道并将该字符串转发到DOS shell.

此外,一旦您将命令发送到DOS shell,请记住,它需要Windows路径(更改目录等时). IE.您必须先将Unix路径转换为Windows路径,然后才能将其发送到DOS shell中.您可以在这样的命令行上自动转换路径...

winepath -w /home/ 2> /dev/null

...产生Z:\home\(例如).另外,以下Python片段将为您做同样的事情:

def convert_unix_path_to_windows_path(in_path):
    cmd = ['winepath', '-w', in_path]
    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    (out, err) = proc.communicate()
    return out.decode('utf-8').strip()

I am trying to write a script in Python which opens wine and then sends code to the wine terminal to open a .exe program. The .exe program is also command driven.

I can open wine, but I can't get any further:

import shlex, subprocess

line = "/usr/bin/open -n -a /Applications/Wine\ Stable.app"
line2 = '''cd /Applications/application/ && wine application.exe /h1 
/k0 Z:/Users/myname/Desktop/File.TXT''' 
line = shlex.split(line)
p1 = subprocess.Popen(line)
p1.stdin.write(line2.encode())

The above doesn't work, wine doesn't seem to receive line2, although

/usr/bin/open -n -a /Applications/Wine\ Stable.app

by itself is fine (it opens Wine but nothing else.)

I'm pretty confused about what the next step should be. I would like to avoid additional dependencies if possible, because it seems simple.

解决方案

The following has worked for me in many cases (on Linux):

import subprocess

command = 'echo "echo foo && echo bar" | wine cmd > std_out.txt 2> std_error.txt &'
subprocess.Popen(command, shell = True)

(I believe wine is also available as a command on MacOS, just like that. Please correct me if I am wrong.)

The command fires up a Windows/DOS-like shell (wine cmd). You can actually type wine cmd into your Linux shell and hit enter - you'll find yourself in a DOS shell. The next step is to get commands into the DOS shell. I do this by piping them into it as a string. In my example, I run two commands: echo foo and echo bar. The initial echo writes the command string to stdout, the following | opens a pipe and forwards the string into the DOS shell.

Besides, once you send commands to the DOS shell, keep in mind that it expects Windows paths (when you change directories etc). I.e. you must translate your Unix paths into Windows paths before you send them into the DOS shell. You can automatically convert your path on a command line like this ...

winepath -w /home/ 2> /dev/null

... resulting in Z:\home\ (for example). Alternatively, the following Python snipped will do the same for you:

def convert_unix_path_to_windows_path(in_path):
    cmd = ['winepath', '-w', in_path]
    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    (out, err) = proc.communicate()
    return out.decode('utf-8').strip()

这篇关于在Mac OSX上从Python 3.6运行wine命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆