欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 美食 > Python脚本:批量修改文件修改时间(带UI界面)

Python脚本:批量修改文件修改时间(带UI界面)

2025/5/6 12:32:27 来源:https://blog.csdn.net/m0_65604669/article/details/147071114  浏览:    关键词:Python脚本:批量修改文件修改时间(带UI界面)
#示例如下

import os
import sys
from datetime import datetime
from PyQt5.QtWidgets import (QApplication, QMainWindow, QVBoxLayout, QHBoxLayout,QWidget, QLabel, QComboBox, QDateTimeEdit, QPushButton,QFileDialog, QCheckBox, QScrollArea, QGroupBox)
from PyQt5.QtCore import QDateTime, Qtclass FileTimeModifier(QMainWindow):def __init__(self):super().__init__()self.setWindowTitle("文件修改时间批量修改工具")self.setGeometry(100, 100, 800, 600)# 主部件和布局self.main_widget = QWidget()self.main_layout = QVBoxLayout()# 顶部控制区域self.control_group = QGroupBox("控制面板")self.control_layout = QHBoxLayout()self.add_button = QPushButton("添加文件")self.add_button.clicked.connect(self.add_files)self.select_all_checkbox = QCheckBox("全选")self.select_all_checkbox.stateChanged.connect(self.toggle_select_all)self.modify_button = QPushButton("应用修改")self.modify_button.clicked.connect(self.modify_files)self.control_layout.addWidget(self.add_button)self.control_layout.addWidget(self.select_all_checkbox)self.control_layout.addWidget(self.modify_button)self.control_group.setLayout(self.control_layout)# 滚动区域用于文件列表self.scroll_area = QScrollArea()self.scroll_widget = QWidget()self.scroll_layout = QVBoxLayout()# 初始文件列表self.file_entries = []self.scroll_widget.setLayout(self.scroll_layout)self.scroll_area.setWidget(self.scroll_widget)self.scroll_area.setWidgetResizable(True)# 添加到主布局self.main_layout.addWidget(self.control_group)self.main_layout.addWidget(self.scroll_area)self.main_widget.setLayout(self.main_layout)self.setCentralWidget(self.main_widget)# 获取当前目录下的文件self.refresh_file_list()def refresh_file_list(self):"""刷新当前目录下的文件列表"""# 清空现有条目for entry in self.file_entries:entry['widget'].setParent(None)self.file_entries.clear()# 获取当前目录下的所有文件current_dir = os.path.dirname(os.path.abspath(__file__))files = [f for f in os.listdir(current_dir) if os.path.isfile(os.path.join(current_dir, f))]# 为每个文件创建条目for file in files:self.add_file_entry(file)def add_file_entry(self, filename):"""为单个文件创建UI条目"""entry_widget = QWidget()entry_layout = QHBoxLayout()# 复选框checkbox = QCheckBox()checkbox.setChecked(True)# 文件名标签file_label = QLabel(filename)file_label.setMinimumWidth(200)# 修改时间选择器time_edit = QDateTimeEdit()time_edit.setDisplayFormat("yyyy-MM-dd HH:mm:ss")time_edit.setDateTime(QDateTime.currentDateTime())# 添加到布局entry_layout.addWidget(checkbox)entry_layout.addWidget(file_label)entry_layout.addWidget(time_edit)entry_widget.setLayout(entry_layout)# 保存条目信息self.file_entries.append({'widget': entry_widget,'checkbox': checkbox,'filename': filename,'time_edit': time_edit})# 添加到滚动区域self.scroll_layout.addWidget(entry_widget)def add_files(self):"""添加文件到列表"""current_dir = os.path.dirname(os.path.abspath(__file__))files, _ = QFileDialog.getOpenFileNames(self, "选择文件", current_dir)for file in files:filename = os.path.basename(file)# 检查是否已存在if not any(entry['filename'] == filename for entry in self.file_entries):self.add_file_entry(filename)def toggle_select_all(self, state):"""全选/取消全选"""for entry in self.file_entries:entry['checkbox'].setChecked(state == Qt.Checked)def modify_files(self):"""修改选中的文件的修改时间"""current_dir = os.path.dirname(os.path.abspath(__file__))modified_count = 0for entry in self.file_entries:if entry['checkbox'].isChecked():filepath = os.path.join(current_dir, entry['filename'])new_time = entry['time_edit'].dateTime().toPyDateTime()# 转换为时间戳timestamp = new_time.timestamp()# 修改文件时间try:os.utime(filepath, (timestamp, timestamp))modified_count += 1except Exception as e:print(f"修改文件 {entry['filename']} 时间失败: {e}")print(f"成功修改了 {modified_count} 个文件的修改时间")if __name__ == "__main__":app = QApplication(sys.argv)window = FileTimeModifier()window.show()sys.exit(app.exec_())

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词