![](/img/trans.png)
[英]Need to restart python in Terminal every time a change is made to script
[英]Python script reading terminal every period of time
我需要创建脚本来做一些事情:
我试图通过仅编写一个命令并读取两次来简化任务来模拟这一点,但它似乎不起作用。 你知道如何解决这个问题吗? 是否有可能以这种方式执行此操作,或者是否有更聪明的方法来处理它?
from subprocess import Popen, PIPE, STDOUT
import pty
import os
master, slave = pty.openpty()
p = Popen('ls', shell=True, stdout=slave, stdin=PIPE, stderr=slave, close_fds=True)
stdin_handle = p.stdin
stdou_handle = os.fdopen(master)
stdin_handle.write('cd /')
stdin_handle.write('ls')
print stdout_handle.readline()
stdin_handle.write('clear')
stdin_handle.write('cd /home')
stdin_handle.write('ls')
print stdout_handle.readline()
stdin_handle.write('exit')
我认为您应该执行如下所示的代码:
import os
import time
while True:
start_time = time.time()
# can check if you are in correct working directory: os.getcwd()
os.chdir('/')
os.listdir()
print(f'\33c\e[3J')
os.chdir('/home')
os.listdir()
print(f'\33c\e[3J')
time.sleep(1 - ((time.time() - start_time) % 1))
# exit due to some conditional for example: if datetime.now().hour == 9:
exit()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.