Python,无法打开文本文件 [英] Python, cannot open a text file

查看:128
本文介绍了Python,无法打开文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 android.txt 的文本文件(有几千行),我尝试用 python 打开它,我的代码是:

I have a text file named android.txt (with a couple of thousand of lines) which I try to open with python, my code is:

f = open('/home/user/android.txt', 'r')

但是当我在做的时候:

f.read()

结果是:

''

我 chmod 777/home/user/android.txt 但结果还是一样

I chmod 777 /home/user/android.txt but the result remains the same

推荐答案

结果将是空字符串而不是空列表,这是因为您的文件大小大于 你的内存(基于你的python版本和你的机器)!所以python并没有将文件内容赋值给一个变量!

The result would be empty string not empty list and it's because your file size is larger than your memory(based on your python version and your machine)! So python doesn't assigned the file content to a variable!

要解决这个问题,您需要逐行处理您的文件.

For getting rid of this problem you need to process your file line by line.

with open('/home/user/android.txt') as f :
    for line in f:
         #do stuff with line

这篇关于Python,无法打开文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆