我正在尝试做类似的事情:
f = subprocess.check_output("./script.sh ls -l test1/test2/test.log", shell=True)
当我打印f时,我得到的值为0。我尝试使用子进程,然后使用read(),即使这样我也没有得到文件的详细信息。我需要验证文件的大小。
不知道该怎么做。
有什么帮助吗?
当我使用
f = os.system("./script.sh ls -l test1/test2/test.log")
,我得到了输出但没有保存在f中。诸如stdoutput之类的东西。
更新:我曾经
f = os.popen("./script.sh ls -l test1/test2/test.log 2>&1")
如果我直接在CLI上直接在上面的引号中运行了相同的命令,则可以正常工作,但是如果我在脚本中使用了以上命令或使用了s = f.readline()
,脚本将停止,则需要先单击“ return”,然后脚本才能继续。
这是为什么?我需要's',因为我需要对其进行处理。
您可以使用subprocess.check_output:
f = subprocess.check_output("./script.sh ls -l test1/test2/test.log",shell=True)
print(f)
您可以不使用shell = True拆分为单个args的列表:
f = subprocess.check_output(['./script.sh', 'ls', '-l', 'test1/test2/test.log']))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句