Python 使用子进程或类似程序将shell命令的标准输出重定向到动态目录/文件名

Python 使用子进程或类似程序将shell命令的标准输出重定向到动态目录/文件名,python,Python,我已经编写了几个月的代码,但遇到了麻烦。 下面的代码只是打印一个菜单,运行一些shell命令并将它们输出到屏幕。 我想做的是将菜单1选项的输出发送到“工作区”。 我将工作区称为用户输入的目录,在执行shell命令时,它会将std作为文件保存到相应的目录中。 我将有超过50个不同的命令,将最终执行,我希望他们都整齐地存储在相对目录。 目前正在使用Python 3.4 到目前为止,我的代码可以请求用户输入工作区,inturn将创建一个相对目录。 我需要的是得到一个要输出的文件 #Imports im

我已经编写了几个月的代码,但遇到了麻烦。 下面的代码只是打印一个菜单,运行一些shell命令并将它们输出到屏幕。 我想做的是将菜单1选项的输出发送到“工作区”。 我将工作区称为用户输入的目录,在执行shell命令时,它会将std作为文件保存到相应的目录中。 我将有超过50个不同的命令,将最终执行,我希望他们都整齐地存储在相对目录。 目前正在使用Python 3.4

到目前为止,我的代码可以请求用户输入工作区,inturn将创建一个相对目录。 我需要的是得到一个要输出的文件

#Imports
import os
import subprocess


#Set Globals for Workspace to 0
workspace = 0
absolute_path = 0


#Initally clear the screen
os.system('clear')

#Define Option 0 - Create a Workspace
def workspace_menu():
    print ("Enter the name of the Workspace or type 'q' or 'quit' to return to the main menu")
    print ("")
    workspace_input = input(":")
    if workspace_input in ("q", "quit"):
        os.system('clear')
    else:
#Define the current working directoy (__file__)
    script_dir = os.path.dirname(__file__)
    relative_path = 'workspaces/'
    joined_path = os.path.join(script_dir, relative_path)
    os.chdir(joined_path)
    if os.path.exists(workspace_input):
        print ("Directory already Exists! - Try a different name")
        input("Press any key to Return to the Main Menu")
        os.system('clear')
    else:
        make_path = os.makedirs(workspace_input)
        absolute_path = joined_path + workspace_input
        global absolute_path
        absolute_path = absolute_path
        global workspace
        workspace = 1
        print ("Workspace created!"), absolute_path
        input("Press any Key to Continue")
        os.system('clear')
        return

#Define the Main Menu
def menu():
    print(" 0) Add a Workspace")
    print(" 1) System Tasks")
    print("11) Exit")


#Define System Tasks
def system_tasks():
    os.system('clear')
    print(" 1) Display Local Network information")
    print(" 5) Back")
    system_tasks_answer = int(input(":"))
    if system_tasks_answer == 1:
        print("The Local Network Configuration of this OS are:")
        print("")
        ifconfig = subprocess.call(['/sbin/ifconfig'])
        dns = subprocess.call(['cat', '/etc/resolv.conf'])
        print("")
        print(workspace)
        lni_menu = input("Press any Key to Continue")
        system_tasks()
        os.system('clear')
    elif system_tasks_answer == 5:
        os.system('clear')


loop=True

while loop:
    print (menu())
    mm_answer = int(input(":"))
    if mm_answer ==0:
        workspace_menu()
    elif mm_answer ==1:
        system_tasks()
    elif mm_answer ==11:
        break
    else:
        input("You did not give a valid answer, press any key to try again") 
        os.system('clear')

您可以使用
subprocess.call
stdout
参数运行命令并将其标准输出重定向到文件。美国

stdin、stdout和stderr分别指定执行程序的标准输入、标准输出和标准错误文件句柄。有效值包括PIPE、DEVNULL、现有文件描述符(正整数)、现有文件对象和None。[……]

这意味着,如果将文件对象(由
open()
返回)传递到
stdout
,它将使用此文件对象重定向标准输出。子进程写入其标准输出的所有内容都会写入文件对象

例如,要将命令
ls/tmp
的输出重定向到文件
output
,您可以编写:

with open("output", "wb") as f:
    return_code = subprocess.call(["ls", "/tmp"], stdout=f)

您可以使用
子流程。调用
stdout
参数来运行命令并将其标准输出重定向到文件。美国

stdin、stdout和stderr分别指定执行程序的标准输入、标准输出和标准错误文件句柄。有效值包括PIPE、DEVNULL、现有文件描述符(正整数)、现有文件对象和None。[……]

这意味着,如果将文件对象(由
open()
返回)传递到
stdout
,它将使用此文件对象重定向标准输出。子进程写入其标准输出的所有内容都会写入文件对象

例如,要将命令
ls/tmp
的输出重定向到文件
output
,您可以编写:

with open("output", "wb") as f:
    return_code = subprocess.call(["ls", "/tmp"], stdout=f)

顺便说一句。有一个整洁的软件包可以让创建提示导向的程序变得非常简单:顺便说一句。有一个整洁的软件包可以让创建提示导向的程序变得非常简单:谢谢你的评论。请你详细解释一下好吗?现在答案应该更详细了。谢谢你的评论。请你详细说明一下好吗?现在答案应该更详细了。