Python不读取文件,返回空结果 [英] Python not reading file, returning empty result

查看:105
本文介绍了Python不读取文件,返回空结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

aaa.txt:

aszczx
d
as
w
ad

python 脚本:

f = open('aaa.txt','r')
f.read()
f.close()

控制台:

C:\Python27>test.py

C:\Python27>

为什么不显示文件内容?

Why is it not displaying the contents of the file?

谢谢,

推荐答案

你不是在显示文件的内容,只是在阅读它.

You are not displaying the contents of the file, just reading it.

例如,您可以执行以下操作:

For instance you could do something like this:

with open('aaa.txt') as infp:
    data = infp.read()

print data # display data read 

使用 with 也会自动关闭文件

Using with will also close the file for your automatically

这篇关于Python不读取文件,返回空结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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