2013-04-23 42 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。

相關問題