需要运行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
答案 0 :(得分:0)
我不确定我是否理解您的问题以及所需的结果,但是要在/dev/pts/5
中生成文件名列表,然后将其另存为.txt
中的/dev/pts/6
文件中,使用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
,然后您必须访问.stdout
对象的CompletedProcess
!