Home  >  Article  >  Backend Development  >  How to determine if a file is empty in Python

How to determine if a file is empty in Python

藏色散人
藏色散人Original
2019-10-17 15:43:4610022browse

How to determine if a file is empty in Python

How does python determine if a file is empty?

Get the file size. If the file size is equal to 0, it is empty, as follows:

import os
 
size = os.path.getsize('d:/abc.txt')
 
if size == 0:
    print('文件是空的')
else:
    print('文件不是空的')

Related recommendations: "Python Tutorial"

Usage of Python function os.path.getsize()

Get the file size:

os.path.getsize(filename)

The above is the detailed content of How to determine if a file is empty in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn