使用'start'从python子进程调用写入输出到文件

时间:2016-03-30 07:13:47

标签: python file subprocess call stdout

我正在尝试将从python脚本打开的shell窗口的输出重定向到文件。

所以,经过一些谷歌搜索,我想出了这个代码:

file_output = open(MS_Log + 'LOGS.txt', 'w+')
subprocess.call('start ' + URL_logs, shell=True, stdout=file_output)

这确实会打开一个新的shell窗口并运行我想要的命令。但是,输出将打印在屏幕上(在新的shell窗口内)而不是文件。

我希望它是两者 - 在shell窗口中打印并保存到文件中。

现在,我在那里运行的命令是连续的,它运行直到用Ctrl + C停止,所以我认为这可能是问题......?也许如果它会优雅地关闭它会将它的内容转储到文件中吗?

2 个答案:

答案 0 :(得分:0)

您可以尝试将输出保存在变量中,然后将其写入文件中:

process = subprocess.Popen(command, stdout = subprocess.PIPE, shell = True)
(process_output,  error) = process.communicate()
file = open(path_of_file_output, "w")
file.write(process_output)
file.close()

答案 1 :(得分:0)

OP,我从this question改编了一个很好的解决方案。

它应该在终端中显示命令的输出,并在运行时将其保存到文件中。这意味着它也适用于无限循环。它没有使用允许shell注入的function getObject(object, path) { function getPart(o) { if (!path.indexOf(o.id)) { if (path === o.id) { result = o; return true; } return Array.isArray(o.item) && o.item.some(getPart); } return false; } var parts = path.split('-'), result; getPart(object); return result; } var object = { "id": 0, "item": [{ "id": "0-", "text": "BlueWing", "userdata": [{ "name": "cid", "content": "10377" }], "item": [{ "id": "0-0", "text": "Auto", "userdata": [{ "name": "cid", "content": "10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388,10533" }], "item": [{ "id": "0-0-0", "text": "Auto", "userdata": [{ "name": "cid", "content": "10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388" }], "item": [{ "id": "0-0-0-0", "text": "testaccount", "userdata": [{ "name": "cid", "content": "10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388" }], "item": [{ "id": "0-0-0-0-0", "text": "Add New List", "userdata": [{ "name": "clid", "content": 99999 }, { "name": "cid", "content": "10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388" }] }, { "id": "0-0-0-0-1", "text": "testaccount", "userdata": [{ "name": "cid", "content": "10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388" }], "item": [{ "id": "0-0-0-0-1-0", "text": "Add New List", "userdata": [{ "name": "clid", "content": 99999 }, { "name": "cid", "content": "10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388" }] }, { "id": "0-0-0-0-1-1", "text": "testaccount - 10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388", "userdata": [{ "name": "cid", "content": "10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388" }], "item": [{ "id": "0-0-0-0-1-1-0", "text": "Add New List", "userdata": [{ "name": "clid", "content": 99999 }, { "name": "cid", "content": "10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388" }] }] }] }] }] }, { "id": "0-0-1", "text": "Balise Toyota", "userdata": [{ "name": "cid", "content": "10533" }], "item": [{ "id": "0-0-1-0", "text": "Balise Toyota", "userdata": [{ "name": "cid", "content": "10533" }], "item": [{ "id": "0-0-1-0-0", "text": "Add New List", "userdata": [{ "name": "clid", "content": 99999 }, { "name": "cid", "content": "10533" }] }, { "id": "0-0-1-0-1", "text": "Balise Toyota", "userdata": [{ "name": "cid", "content": "10533" }], "item": [{ "id": "0-0-1-0-1-0", "text": "Add New List", "userdata": [{ "name": "clid", "content": 99999 }, { "name": "cid", "content": "10533" }] }, { "id": "0-0-1-0-1-1", "text": "Balise Toyota - 10533", "userdata": [{ "name": "cid", "content": "10533" }], "item": [{ "id": "0-0-1-0-1-1-0", "text": "Add New List", "userdata": [{ "name": "clid", "content": 99999 }, { "name": "cid", "content": "10533" }] }, { "id": "0-0-1-0-1-1-1", "text": "Balise.List - 11949", "userdata": [{ "name": "clid", "content": 11949 }, { "name": "cid", "content": "10533" }] }] }] }] }] }] }, { "id": "0-1", "text": "BlueWing", "userdata": [{ "name": "cid", "content": "10667,10668" }], "item": [{ "id": "0-1-0", "text": "HVAC", "userdata": [{ "name": "cid", "content": "10667,10668,10348,10294,10297,10313,10521,10351,10309,10296,10316,10315,10312,10301,10347,10300,10306,10308,10307,10298,10305,10371,10291,10349,10311,10354,10345,10310,10292,10372,10295,10304,10303,10299,10314,10290,10490,10293,10302" }], "item": [{ "id": "0-1-0-0", "text": "HVAC", "userdata": [{ "name": "cid", "content": "10667,10668" }], "item": [{ "id": "0-1-0-0-0", "text": "Add New List", "userdata": [{ "name": "clid", "content": 99999 }, { "name": "cid", "content": "10667,10668" }] }, { "id": "0-1-0-0-1", "text": "Mann Plumbing & Heating", "userdata": [{ "name": "cid", "content": "10667" }], "item": [{ "id": "0-1-0-0-1-0", "text": "Add New List", "userdata": [{ "name": "clid", "content": 99999 }, { "name": "cid", "content": "10667" }] }, { "id": "0-1-0-0-1-1", "text": "Mann Plumbing & Heating - 10667", "userdata": [{ "name": "cid", "content": "10667" }], "item": [{ "id": "0-1-0-0-1-1-0", "text": "Add New List", "userdata": [{ "name": "clid", "content": 99999 }, { "name": "cid", "content": "10667" }] }, { "id": "0-1-0-0-1-1-1", "text": "Radius List - 12960", "userdata": [{ "name": "clid", "content": 12960 }, { "name": "cid", "content": "10667" }] }] }] }, { "id": "0-1-0-0-2", "text": "RES Air Conditioning", "userdata": [{ "name": "cid", "content": "10668" }], "item": [{ "id": "0-1-0-0-2-0", "text": "Add New List", "userdata": [{ "name": "clid", "content": 99999 }, { "name": "cid", "content": "10668" }] }, { "id": "0-1-0-0-2-1", "text": "RES Air Conditioning - 10668", "userdata": [{ "name": "cid", "content": "10668" }], "item": [{ "id": "0-1-0-0-2-1-0", "text": "Add New List", "userdata": [{ "name": "clid", "content": 99999 }, { "name": "cid", "content": "10668" }] }] }] }] }] }] }] }] }, id = "0-1-0-0-0", part = getObject(object, id); document.write('<pre>' + JSON.stringify(part, 0, 4) + '</pre>');选项(如果您不只是在家中的LAN上播放,则始终对使用shell=True的脚本设置严格权限)。 该解决方案有一个缺点:只有在输出中有换行符时它才有效;如果没有,将不会打印/写入任何内容。

shell=True