1
有沒有辦法在Python中檢查文本文件是否爲空而不使用操作系統?檢查文本文件是否爲空Python
我已經試過到目前爲止
x = open("friends.txt")
friendsfile = x.readlines()
if friendsfile == None
但我不認爲這是正確的做法。
有沒有辦法在Python中檢查文本文件是否爲空而不使用操作系統?檢查文本文件是否爲空Python
我已經試過到目前爲止
x = open("friends.txt")
friendsfile = x.readlines()
if friendsfile == None
但我不認爲這是正確的做法。
不知道你爲什麼不使用os
,但我想如果你真的想要你可以打開文件並獲得第一個字符。
with open('friends.txt') as friendsfile:
first = friendsfile.read(1)
if not first:
print('friendsfile is empty')
else:
#do something
爲什麼你不能使用os? – Radan
該解決方案位於@Andy提供的鏈接中。重複確認。 –
不,我不認爲有鏈接的解決方案,請注意他不想使用操作系統。 – Radan