[英]How can I correct count lines in text file in Python and get its size in bytes?
I have trouble to get correct values for an exercise with the following instructions.我无法按照以下说明获得正确的练习值。 Write a function that open a file for reading and returns the number of bytes and newlines('\\n').
编写一个函数,打开一个文件进行读取并返回字节数和换行符('\\n')。
I should get values for def readFile(tmp.txt)
is (12, 4)
, but I got (11, 5)
.我应该得到
def readFile(tmp.txt)
是(12, 4)
,但我得到了(11, 5)
。
Where I am doing wrong and could you explain me in great details why is that.我哪里做错了,你能详细解释一下为什么会这样。
def readFile(filename):
f = open(filename, 'r')
size = 0 # Total size in bytes of all lines in a text file
lines = 0 # Total number of lines
buf = f.readline() # Read a line
while buf != "":
buf = f.readline() # Read a line
size += len(buf)
lines += 1 # Count lines
f.close # Close a file
return (size, lines)
os.path.getsize(filename) will return the number of bytes, see here . os.path.getsize(filename) 将返回字节数,请参见 此处。 With file.read() the entire contents of the .txt file can be read and returned, see here .
使用 file.read() 可以读取和返回 .txt 文件的全部内容,请参见此处。 You can then use the method .count("\\n") to count the number of occurrences of \\n.
然后,您可以使用 .count("\\n") 方法来计算 \\n 的出现次数。 I recommend reading the paragraphs on .close() and using the with keyword as well (see previous link).
我建议阅读 .close() 上的段落并使用 with 关键字(参见上一个链接)。
Note: The following code snippets assume that tmp.txt is in the same folder as the .py file.注意:以下代码片段假定 tmp.txt 与 .py 文件位于同一文件夹中。
import os
def read_file(filename):
nr_of_bytes = os.path.getsize(filename)
with open(filename, "r") as file:
nr_of_newlines = file.read().count("\n")
return nr_of_bytes, nr_of_newlines
print(read_file("tmp.txt"))
Shorter version:较短的版本:
import os
def read_file(filename):
with open(filename, "r") as file:
nr_of_newlines = file.read().count("\n")
return os.path.getsize(filename), nr_of_newlines
print(read_file("tmp.txt"))
Finaly I managed to get correct result.最后我设法得到正确的结果。 Here is a code, perhaps with unusual approach since some inbuilt functions mentioned above does not work while coding in Pyschool website.
这是一个代码,可能采用了不寻常的方法,因为在 Pyschool 网站上编码时,上面提到的一些内置功能不起作用。
def readFile(filename):
f = open(filename, 'r')
string1 = f.read() # Read file
size = len(string1) # Getting file size, assuming length of a string represent
# file size (python 2.x)
f.close # We close file temporarily, once we read all bytes,
# we cannot read file again (based on my experience with Python, perhaps I am wrong)
d = open(filename, 'r') # Again we open same file
lines = d.read().count("\n") # Counting '\n'
d.close # We close file
return (size, lines) # Return result
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.