Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.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
python脚本输出和python控制台输出之间的差异_Python_Console_Raw Input - Fatal编程技术网

python脚本输出和python控制台输出之间的差异

python脚本输出和python控制台输出之间的差异,python,console,raw-input,Python,Console,Raw Input,我有一个.py文件: from sys import argv script, filename = argv print "We're going to erase %r." % filename print "If you don't want that, hit CTRL-C (^C)." print "If you do want that, hit RETURN." raw_input("?") print "Opening the file..." target = open

我有一个.py文件:

from sys import argv

script, filename = argv

print "We're going to erase %r." % filename
print "If you don't want that, hit CTRL-C (^C)."
print "If you do want that, hit RETURN."

raw_input("?")

print "Opening the file..."
target = open(filename, 'w')

print "Truncating the file.  Goodbye!"
target.truncate()

print "Now I'm going to ask you for three lines."

line1 = raw_input("line 1: ")
line2 = raw_input("line 2: ")
line3 = raw_input("line 3: ")

print "I'm going to write these to the file."

target.write(line1)
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")

print "And finally, we close it."
target.close()
我不明白的是:

a) 为什么在解释器中,如果我写,
raw\u input(“?”
),然后键入
f
并按enter键,它会输出
'f'
字符串,如果我运行.py文件,它不会返回'f'字符串

b) 此外,python文档说:“然后函数从输入中读取一行,将其转换为字符串(去掉尾随的换行符),并返回该字符串。那么,为什么第7行打印在新行而不是第6行(“打开文件…”)上呢。该
\n
来自何处?

a)默认情况下,解释器会打印命令的输出,但您的脚本不会这样做,除非您使用
print
语句

print raw_input('?')
b)
'\n'
不在从
raw\u input
返回的字符串中,但当您按enter键时,控制台会捕获该字符串,因此这是使用
raw\u input
时产生的副作用

print repr(raw_input('?'))  # You'll get 'f', not 'f\n'
a) 它确实返回字符串,但您没有将其保存在变量中。在这种情况下,交互式解释器将回显该值

b)
\n
可能是输入(您的键入)的一部分,但很难知道您的确切意思。

您所说的“it not return me f”是什么意思?目标文件的编写与您预期的一样?