2016-04-15 73 views
1

有没有办法在Python中检查文本文件是否为空而不使用操作系统?检查文本文件是否为空Python

我已经试过到目前为止

x = open("friends.txt") 
friendsfile = x.readlines() 

if friendsfile == None 

但我不认为这是正确的做法。

+0

为什么你不能使用os? – Radan

+0

该解决方案位于@Andy提供的链接中。重复确认。 –

+1

不,我不认为有链接的解决方案,请注意他不想使用操作系统。 – Radan

回答

3

不知道你为什么不使用os,但我想如果你真的想要你可以打开文件并获得第一个字符。

with open('friends.txt') as friendsfile: 
    first = friendsfile.read(1) 
    if not first: 
     print('friendsfile is empty') 
    else: 
     #do something