[英]Python: How can I obtain the filesize of a selected file in PyQt5?
In PyQt5, it is possible to select a file using QFileDialog. 在PyQt5中,可以使用QFileDialog选择文件。 I understand how to obtain the file name, but how might one obtain the filesize?
我知道如何获取文件名,但是如何获取文件大小?
Without opening the file: 不打开文件:
You must use the QFileInfo
class and the size()
method: 您必须使用
QFileInfo
类和size()
方法:
filename, _ = QFileDialog.getOpenFileName(None, 'Open file')
if filename != "":
info = QFileInfo(filename)
size = info.size()
print(info)
Opening the file: 打开文件:
filename, _ = QFileDialog.getOpenFileName(None, 'Open file')
if filename != "":
file = QFile(filename)
if file.open(QFile.ReadOnly):
print(file.size())
From the documentation : 从文档中 :
The file dialog has two view modes ... Detail also displays a list of file and directory names, but provides additional information alongside each name, such as the file size and modification date.
文件对话框有两种查看模式...详细信息还显示文件和目录名称的列表,但是在每个名称旁边提供其他信息,例如文件大小和修改日期。 Set the mode with setViewMode():
使用setViewMode()设置模式:
dialog.setViewMode(QFileDialog::Detail);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.