简体   繁体   English

在 python 上读取文件返回一个空字符串

[英]Reading a file on python returns an empty string

I have been trying to read a file in python, the thing is that it returns an empty string.我一直在尝试在 python 中读取文件,问题是它返回一个空字符串。 Here is the code:这是代码:


with open('data.txt') as file:
    content = f.readlines()
    print(content) #prints nothing

note: It's a really big file, is that a problem?注意:这是一个非常大的文件,有问题吗?

  1. change f.readlines() to file.readlines() This is only a typo.f.readlines()更改为file.readlines()这只是一个错字。 But I don't know why you don't get the error here.但我不知道为什么你在这里没有得到错误。 I think you open another file as f And You get the empty string here 'cause if you try to read a file more than once, you got an empty string except for the first time.我认为你打开另一个文件 f 并且你在这里得到空字符串,因为如果你尝试多次读取一个文件,除了第一次之外,你得到一个空字符串。
  2. As you say the file size is 4TB , This could be the problem because of Max File Size In Python正如你所说的文件大小是4TB ,这可能是因为Max File Size In Python的问题

with open('data.txt') as file:
    content = file.readlines()
    print(content) #prints nothing


This should work i think我认为这应该可行

您需要确保您的文本文件名是正确的,但最重要的是,您需要提供文件的完整路径,以防它与脚本不在同一目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM