有没有办法检查Python中的文本文件是否为空而不使用操作系统?
到目前为止我尝试过的
x = open("friends.txt")
friendsfile = x.readlines()
if friendsfile == None
Run Code Online (Sandbox Code Playgroud)
但我不认为这是正确的方法.
不知道为什么你不会使用os,但我想如果你真的想要你可以打开文件并获得第一个字符.
with open('friends.txt') as friendsfile:
first = friendsfile.read(1)
if not first:
print('friendsfile is empty')
else:
#do something
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14477 次 |
| 最近记录: |