gpt4 book ai didi

python - 如何将 Unix shell 参数的输出分配给 Python 中的变量

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:06:10 25 4
gpt4 key购买 nike

我想通过 subprocess.call() 函数执行一个 unix/linux 实用程序,并将命令的输出存储在一个变量中,以便在程序的其他部分操作和分析命令的输出。正在考虑做的是将输出重定向到文本文件,然后打开文本文件并遍历文件的每一行并将数据输入(存储)到列表中。例如:

#! /usr/bin/python

from subprocess import call

# This listHome.py program has been designed and created as a
# demonstration
# author:oOpSgEoW

class LstHome:

def lsthome(self):
# create the argument that will be passed to the call function
lsthme = 'ls $HOME > HomeList.txt'
# call the function
call(lsthme, shell=True)

def add_both(self):

# create a list and a file object for the file
lstOne = []
fila = open('HomeList.txt', 'r')

# iterate over each line of the file and store the
# data into the each index of the list
for line in fila:
a = line.strip("\n")
lstOne.append(a)

# close the file
fila.close()

# return the list
return lstOne

class HomePrint():

# intialize the class, pass the list as lstAlpha
def __init__(self, lstAlpha=None):
# to keep the bounds of the list, which will initialize
# the list an empty list before the content of the list
# being passed as an argument
if lstAlpha is None:
lstTwo = []
self.lstTwo = lstAlpha
def print_lst(self):
for line1 in self.lstTwo:
print(line1)

def main():

# create an object out of the first class
x = LstHome()

# call the lsthome() function in
# order to execute the command givenper
x.lsthome()

# assign and create an object out of the HomePrint class
# pass the output of the add_both() function from
# the LstHome() class
y = HomePrint(x.add_both())
y.print_lst()

# an exit statement to the user
print 'The $HOME directory of the user has been printed\ndone.'

main()

有没有一种方法可以在我的第一个类的函数中分配 call(lsthme, shell=True),而不是将输出重定向到 HomeList.txt 文件?所以基本上我问的是我可以/可以编码:

lsthme = 'ls $HOME'
holdVar = call(lsthme, shell=True)
print(holdVar)

以上是合法的论据吗?如果不是,什么会产生与我正在尝试做的事情类似的结果?

谢谢


已编辑:为需要 Python 主题的其他人更正示例

#! /usr/bin/python

from subprocess import PIPE, Popen, call

# This listHome.py program has been designed and created to
# demonstrate a multi-class program that has a class receive
# an array/list as a parameter, and demonstrates interacting with a
# Unix shell with a multi-class program
# author:oOpSgEoW

class LstHome:

def lsthome(self):
# create the argument that will be passed to the call function
# Use the Popen function of subprocess
lsthme = Popen("ls $HOME", shell=True, stdout=PIPE)

# assign the function to lstOne
lstOne = lsthme.stdout.read().split('\n')
# now that the data has been stored, the Pipe must be closed
# NOTE: Generally speaking, what goes up must come down. What lives, must die. What opens must eventually close.
lsthme.stdout.close()

# return the lstOne object.
return lstOne

class HomePrint():

# intialize the class, pass the list as lstAlpha
def __init__(self, lstAlpha=None):
# to keep the bounds of the list, which will initialize
# the list an empty list before the content of the list
# being passed as an argument
if lstAlpha is None:
lstTwo = []
self.lstTwo = lstAlpha

def print_lst(self):
for line1 in self.lstTwo:
# NEVER PASS A NEWLINE RETURN TO THE CALL FUNCTION,
# AT THE END OF AN ARGUMENT, just in case you wanted to
# to take the output, or some of the output, and use as a
# command line input. For example:
# if ".py" in line1:
# line2 = line1.strip('\n')
# mover = 'mv '
# newmov = ' $HOME/Documents/Examples_In_Py/'
# doTheMov = mover + line2 + newmov
# call(doTheMov, shell=True)
print(line1)

def main():

# create objects by performing class and functional abstraction
x = LstHome()
x.lsthome()
# pass the list as an argument
y = HomePrint(x.lsthome())
y.print_lst()

print 'The $HOME directory of the user has been printed\ndone.'

main()

最佳答案

您可以将调用方法替换为 Popen !

你的代码最后看起来像这样:

from subprocess import PIPE, Popen

res = Popen("ls $HOME",shell=True, stdout=PIPE)
home_list = res.stdout.read().split('\n')

你会得到一个主文件夹列表

关于python - 如何将 Unix shell 参数的输出分配给 Python 中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40467393/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com