【问题标题】:Is there a function in OS library or another python library that checks what the file extension is [duplicate]OS库或另一个python库中是否有一个函数可以检查文件扩展名是什么[重复]
【发布时间】:2021-01-07 17:47:42
【问题描述】:

我想检查一个文件是否有特定的扩展名,然后像这样重命名它:

import os

file_name = "path to file"
rename = "renamed file"

if file_name_extension == ".png":
   os.rename(file_name, rename)

【问题讨论】:

    标签: python if-statement file-rename


    【解决方案1】:

    Qt 提供了一个强大的文件路径工具类,QFileInfo

    from Pyside2 import QtCore
    ext = QtCore.QFileInfo(full_filepath).suffix()
    
    

    请参阅 :https://doc.qt.io/qt-5/qfileinfo.html 的文档(注意 Qt 文档是用 c++ 编写的,但对于这个问题很容易理解)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-09
      • 1970-01-01
      • 1970-01-01
      • 2015-01-18
      相关资源
      最近更新 更多