Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将StdIn重定向到python manage.py shell时从StdIn读取输入_Python_Django_Shell_Stdin - Fatal编程技术网

将StdIn重定向到python manage.py shell时从StdIn读取输入

将StdIn重定向到python manage.py shell时从StdIn读取输入,python,django,shell,stdin,Python,Django,Shell,Stdin,我有一个python脚本,我称之为重定向到Djangomanage.pyshell $ python manage.py shell < script.py 错误: EOFError: EOF when reading a line 带有sys.stdin.readline: answer = sys.stdin.readline() if answer == 'y': # Do something else: pass 在这种情况下,脚本将继续,而不等待用户输入 正

我有一个python脚本,我称之为重定向到Django
manage.pyshell

$ python manage.py shell < script.py
错误:

EOFError: EOF when reading a line
带有sys.stdin.readline:

answer = sys.stdin.readline()
if answer == 'y':
    # Do something
else:
    pass
在这种情况下,脚本将继续,而不等待用户输入


正确的方法是什么?

您正在制作
/manage.py shell
获取一些输入,但您提供的输入是
script.py
的内容

如果能写一个习惯,那就好多了。例如:

from django.core.management.base import BaseCommand

class Command(BaseCommand):
    def handle(self, *args, **options):
        answer = input('A question')
        if answer == 'y':
            # Do something
        else:
            pass

然后使用
manage.py my_script

调用它。为什么要将字符串“A question”传递给readline()?对不起,我已更正了问题。您是否使用原始输入()而不是输入()进行了尝试在Python 3中,
raw\u input
被重命名为
input
。我认为这个问题与试图从
stdin
获取输入有关,而此时已经在
stdin
流重定向中。原因两个命令似乎都在考虑下一个文件字符。
from django.core.management.base import BaseCommand

class Command(BaseCommand):
    def handle(self, *args, **options):
        answer = input('A question')
        if answer == 'y':
            # Do something
        else:
            pass