我试图用python编写一个非常简单的电子邮件脚本。基本上是个穷人的杂种。在工作中,我们从服务器发送大量数据,直接从服务器发送数据要容易得多。
我被困的部分是处理信息。我希望用户能够执行以下操作:
$ cat message.txt | emailer.py fandingo@example.com
$ tail -n 2000 /var/log/messages | emailer.py fandingo@example.com
这两个都很简单。我只需要sys.stdin.read()
就可以得到我的数据。
我遇到的问题是,我还希望支持按以下用法键入消息的提示:
emailer.py --attach-file /var/log/messages fandingo@example.com
Enter Your message. Use ^D when finished.
>> Steve,
>> See the attached system log. See all those NFS errors around 2300 UTC today.
>>
>> ^D
我遇到的麻烦是,如果我尝试sys.stdin.read()
,但是没有数据,那么我的程序就会阻塞,直到stdin获得数据,但是我无法打印我的提示。
我可以采取一种安全的方法,使用raw_input("Enter Your message. Use ^D when finished.")
而不是stdin.read()
,但是我总是打印提示。
有没有一种方法可以查看用户是否在不使用将阻塞的方法的情况下将文本导入python?
您可以使用
sys.stdin.isatty
检查脚本是否以交互方式运行。示例:相关问题 更多 >
编程相关推荐