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?
            Asked
            
        
        
            Active
            
        
            Viewed 819 times
        
    1
            
            
        - 
                    Do you want to get the file size selected by QFileDialog? – eyllanesc Aug 13 '17 at 19:56
- 
                    1If you have a filename, you can get file size via [methods from python library](https://stackoverflow.com/questions/2104080/how-to-check-file-size-in-python). Or you need something else? – Stanislav Ivanov Aug 13 '17 at 19:58
- 
                    How does one in general get the size of a file? I guess that would also work here. – NoDataDumpNoContribution Aug 17 '17 at 10:45
2 Answers
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
- 
                    Better than a few previous answers. Implementation is something that helps more than simple explanation. – Blue Square Aug 13 '17 at 20:18
- 
                    I recommend taking the time to see if the answer they propose is correct. Comments are not answers. – eyllanesc Aug 13 '17 at 20:22
- 
                    
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
