Linux 使Python文本变为绿色并使用旋转光标-新手问题
我想让我的Python脚本文件,当我向用户宣布某事时,变成绿色,如下所示: 如何做到这一点?我看到一个脚本使用sys.stdout.write,但我不知道如何使用它,我使用一个简单的打印命令 此外,只要此命令运行并且仅当此命令停止时停止,我希望旋转光标旋转: 打印“正在运行网络扫描” 输出=子进程。检查输出'nmap-sL 192.168.1.0/24',shell=True 打印“完成” 在任务完成之前,有没有办法在未知的时间完成任务 我正在使用此处建议的代码:Linux 使Python文本变为绿色并使用旋转光标-新手问题,linux,multithreading,python-2.7,time,subprocess,Linux,Multithreading,Python 2.7,Time,Subprocess,我想让我的Python脚本文件,当我向用户宣布某事时,变成绿色,如下所示: 如何做到这一点?我看到一个脚本使用sys.stdout.write,但我不知道如何使用它,我使用一个简单的打印命令 此外,只要此命令运行并且仅当此命令停止时停止,我希望旋转光标旋转: 打印“正在运行网络扫描” 输出=子进程。检查输出'nmap-sL 192.168.1.0/24',shell=True 打印“完成” 在任务完成之前,有没有办法在未知的时间完成任务 我正在使用此处建议的代码: 所以,关于将终端颜色设置为绿色
所以,关于将终端颜色设置为绿色,有一个整洁的软件包,它通常对我非常有用。要检查进程是否正在运行,我建议使用Popen而不是check_输出,因为据我所知,后者不允许您与进程通信。但您需要这样做,因为您想知道您的子流程是否仍在运行。下面是一个小代码示例,可以让您运行:
import subprocess
import shlex
import time
import sys
import colorama
def spinning_cursor():
"""Spinner taken from http://stackoverflow.com/questions/4995733/how-to-create-a-spinning-command-line-cursor-using-python/4995896#4995896."""
while True:
for cursor in '|/-\\':
yield cursor
# Create spinner
spinner = spinning_cursor()
# Print and change color to green
print(colorama.Fore.GREEN + 'running network scan')
# Define command we want to run
cmd = 'your command goes here'
# Split args for POpen
args=shlex.split(cmd)
# Create subprocess
p = subprocess.Popen(args,stdout=subprocess.PIPE)
# Check if process is still running
while p.poll()==None:
# Print spinner
sys.stdout.write(spinner.next())
sys.stdout.flush()
sys.stdout.write('\b')
print('Done')
# Grab output
output=p.communicate()[0]
# Reset color (otherwise your terminal is green)
print(colorama.Style.RESET_ALL)