Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.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 2.7 - Fatal编程技术网

Python代码乱七八糟

Python代码乱七八糟,python,python-2.7,Python,Python 2.7,你好,谢谢你抽出时间。 最近我一直在尝试通过 Zed A.Shaw的《学习python的艰难之路》 在其中一个练习中,我遇到了一个小问题,但这是一个令人烦恼的问题 代码如下所示: from sys import argv script, input_file = argv def print_all(f): print f.read() def rewind(f): f.seek(0) def print_a_line(line_count, f): print l

你好,谢谢你抽出时间。 最近我一直在尝试通过 Zed A.Shaw的《学习python的艰难之路》

在其中一个练习中,我遇到了一个小问题,但这是一个令人烦恼的问题

代码如下所示:

from sys import argv
script, input_file = argv

def print_all(f):
    print f.read()

def rewind(f):
    f.seek(0)

def print_a_line(line_count, f):
    print line_count, f.readline()

current_file = open(input_file)
print "First let's print the whole file:\n"
print_all(current_file)
print "now let's rewind it kind off like a tape."
rewind(current_file)
print "let's print three lines."
current_line = 1 
print_a_line(current_line, current_file)
current_line += 1
print_a_line(current_file, current_file)
current_line += 1
print_a_line(current_line, current_file)
First let's print the whole file:

To all the people out there.
I say I don't like my hair.
I need to shave it off.

Now let's rewind it kind off like a tape.
let's print three lines.
1 To all the people out there.

<open file 'test.txt', mode 'r' at 0x01D16230> I say I don't like my hair.

3 I need to shave it off.
结果是:

from sys import argv
script, input_file = argv

def print_all(f):
    print f.read()

def rewind(f):
    f.seek(0)

def print_a_line(line_count, f):
    print line_count, f.readline()

current_file = open(input_file)
print "First let's print the whole file:\n"
print_all(current_file)
print "now let's rewind it kind off like a tape."
rewind(current_file)
print "let's print three lines."
current_line = 1 
print_a_line(current_line, current_file)
current_line += 1
print_a_line(current_file, current_file)
current_line += 1
print_a_line(current_line, current_file)
First let's print the whole file:

To all the people out there.
I say I don't like my hair.
I need to shave it off.

Now let's rewind it kind off like a tape.
let's print three lines.
1 To all the people out there.

<open file 'test.txt', mode 'r' at 0x01D16230> I say I don't like my hair.

3 I need to shave it off.
首先,让我们打印整个文件:
给所有的人。
我说我不喜欢我的头发。
我需要把它剃掉。
现在让我们像磁带一样倒带。
我们打印三行。
向所有在场的人致意。
我说我不喜欢我的头发。
我需要把它刮掉。
除了
,一切都很好,在我过于简单的头脑中,它似乎是随机的

为什么会出现这种情况?

注意:我在Windows8.1上使用记事本+

print_a_line(current_file, current_file)
这应该是

print_a_line(current_line, current_file)
您看到的输出是由于

print line_count

其中line\u count实际上是指先前打开以进行读取的文件流。

您得到的是
current\u file
,而不是
current\u line
Wow。。谢谢你,我发誓我已经看过代码好几次了,我不知道我是怎么漏掉的。