1

In PyQt5, it is possible to select a file using QFileDialog. I understand how to obtain the file name, but how might one obtain the filesize?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241

2 Answers2

4

Without opening the file:

You must use the QFileInfo class and the size() method:

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())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
1

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():

dialog.setViewMode(QFileDialog::Detail);

Milk
  • 2,469
  • 5
  • 31
  • 54