簡體   English   中英

Python:如何在PyQt5中獲取所選文件的文件大小?

[英]Python: How can I obtain the filesize of a selected file in PyQt5?

在PyQt5中,可以使用QFileDialog選擇文件。 我知道如何獲取文件名,但是如何獲取文件大小?

不打開文件:

您必須使用QFileInfo類和size()方法:

filename, _ = QFileDialog.getOpenFileName(None, 'Open file')
if filename != "":
    info = QFileInfo(filename)
    size = info.size()
    print(info)

打開文件:

filename, _ = QFileDialog.getOpenFileName(None, 'Open file')
if filename != "":
    file = QFile(filename)
    if file.open(QFile.ReadOnly):
        print(file.size())

文檔中

文件對話框有兩種查看模式...詳細信息還顯示文件和目錄名稱的列表,但是在每個名稱旁邊提供其他信息,例如文件大小和修改日期。 使用setViewMode()設置模式:

dialog.setViewMode(QFileDialog::Detail);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM