我需要运行一个python脚本来执行以下操作,我现在正在手动执行此操作以进行测试
cat /dev/pts/5然后,我需要将其回显到/dev/pts/6
echo <DATA_RECEIVED_FROM_5> > /dev/pts/6我似乎无法让python真正读取来自/dev/pts/5的内容,并将其保存到一个列表中,然后使用echo逐个输出到/dev/pts/6
#!/bin/python
import sys
import subprocess
seq = []
count = 1
while True:
term = subprocess.call(['cat','/dev/pts/5'])
seq.append(term)
if len(seq) == count:
for i in seq:
subprocess.call(['echo',i,'/dev/pts/6'])
seq = []
count = count + 1发布于 2019-04-06 03:43:14
我不确定我是否理解您的问题和期望的结果,但是要在/dev/pts/5中生成文件名列表并将其保存为/dev/pts/6中的.txt文件,您应该使用python标准附带的os模块。您可以按如下方式完成此任务:
import os
list_of_files = []
for dirpath, dirnames, filenames in os.walk('/dev/pts/5'):
list_of_files.append([dirpath, dirnames, filenames])
with open('/dev/pts/6/output.txt', 'w+') as file:
for file_info in list_of_files:
file.write("{} -> {} -> {}".format(file_info[0], file_info[1], file_info[2]))这样的输出可能会有点多,但您可以只应用一些逻辑来过滤出您正在寻找的内容。
更新
在python中从任意文件读取数据并将其写入任意文件(没有扩展名)是非常容易的(如果我理解正确的话):
with open('/dev/pts/5', 'rb') as file: # use 'rb' to handle arbitrary data formats
data = file.read()
with open(''/dev/pts/6', 'wb+') as file:
# 'wb+' will handle arbitrary data and make file if it doesn't exist.
# if it does exist it will be overwritten!! To append instead use 'ab+'
file.write(data)第三次是魅力
根据示例here,您似乎需要运行以下命令:
term = subprocess.run(['cat','/dev/pts/5'], capture_output=True)
print(term.stdout)其中重要的部分是capture_output=True,然后您必须访问CompletedProcess对象的.stdout!
https://stackoverflow.com/questions/55542162
复制相似问题