2013-04-23 41 views
1

我有一些代码,有时会从命令行(django管理命令)运行,有时不会(django changelist action)。在这段代码中,如果发生某种异常,我可以得到一些用户输入,并在命令提示符(stdin)可用时继续执行。否则,我需要让异常传播或做一些不同的事情。有没有办法来检测命令提示符在python/django中是否可用?

例如

def copy_account_settings(old_acct_domain, new_acct_domain): 
    try: 
    new_account = Account.objects.get(domain = new_acct_domain) 
    except Account.DoesNotExist: 
    print ("Couldn't find an account matching %s." % new_acct_domain) 
    if <command prompt is available>: 
     print "Would you like to create the account? (y/n)" 
     if raw_input().lower().strip()='y': 
     # get some more input and create the account and move on 
    else: 
     raise 

你会怎么做?

回答

0

也许你可以检查一个TTY?

import os 
if os.isatty(0): 

如果会话为交互式,则返回true,否则返回false。

相关问题