最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python批量實現(xiàn)給文件夾添加文件

 更新時間:2025年09月24日 09:59:45   作者:阿幸軟件雜貨間  
這篇文章主要為大家詳細介紹了如何使用Python批量實現(xiàn)給文件夾添加文件功能,文中的示例代碼講解詳細,有需要的小伙伴可以跟隨小編一起學習一下

代碼功能介紹

這個代碼的功能就是一個,給某個文件夾里面添加某個文件(含父級文件夾下的每一個子文件夾)

舉個例子,父級文件夾是:“D:\Desktop\1,要添加的文件路徑是:D:\1.txt”

則最后會把文件1.txt復制到文件夾D:\Desktop\1里面去。

代碼界面截圖

完整代碼

下面是現(xiàn)成的源碼

import sys
import os
import shutil
import webbrowser
from PyQt5.QtWidgets import (QApplication, QMainWindow, QPushButton, QLabel, 
                            QLineEdit, QVBoxLayout, QHBoxLayout, QWidget, 
                            QTextEdit, QMessageBox, QFileDialog)
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QFont, QPalette, QColor

class FileCopyTool(QMainWindow):
    def __init__(self):
        super().__init__()
        # 定義顏色常量
        self.BUTTON_BLUE = QColor(33, 150, 243)     # 按鈕藍色(#2196F3)
        self.BUTTON_GREEN = QColor(76, 175, 80)     # 按鈕綠色(#4CAF50)
        self.BUTTON_RED = QColor(244, 67, 54)       # 按鈕紅色(#f44336)
        self.LINE_EDIT_BG = QColor(245, 245, 245)   # 文本框背景色(淺灰色)
        self.folder_list = []  # 存儲多個文件夾路徑
        self.initUI()
        
    def initUI(self):
        # 設置窗口標題和大小
        self.setWindowTitle('文件批量復制工具@阿幸')
        self.setGeometry(100, 100, 900, 600)
        self.setMinimumSize(800, 500)
        
        # 創(chuàng)建中心部件
        central_widget = QWidget()
        self.setCentralWidget(central_widget)
        
        # 主布局
        main_layout = QVBoxLayout(central_widget)
        main_layout.setContentsMargins(15, 15, 15, 15)
        main_layout.setSpacing(12)
        
        # 批量文件夾區(qū)域
        batch_folder_layout = QHBoxLayout()
        batch_folder_layout.setSpacing(10)
        
        self.batch_folder_label = QLabel('文件夾路徑:')
        self.batch_folder_label.setFont(QFont("Microsoft YaHei", 10))
        self.batch_folder_label.setFixedWidth(100)
        self.batch_folder_label.setAlignment(Qt.AlignVCenter | Qt.AlignRight)
        
        self.batch_folder_edit = QLineEdit()
        self.setup_line_edit_bg(self.batch_folder_edit)
        self.batch_folder_edit.setFont(QFont("Microsoft YaHei", 10))
        # 設置默認文件夾路徑
        self.default_folder = r"D:\Desktop\文件存儲"
        # 檢查默認路徑是否存在
        if os.path.exists(self.default_folder):
            self.folder_list = [self.default_folder]
            self.batch_folder_edit.setText(self.default_folder)
        else:
            self.batch_folder_edit.setPlaceholderText("已選擇 0 個文件夾")
        self.batch_folder_edit.setReadOnly(True)
        
        self.batch_folder_btn = self.create_browse_button("瀏覽", self.select_batch_folders, self.BUTTON_GREEN)
        
        self.clear_batch_btn = self.create_browse_button("清空", self.clear_batch_folders, self.BUTTON_RED)
        
        batch_folder_layout.addWidget(self.batch_folder_label)
        batch_folder_layout.addWidget(self.batch_folder_edit, 1)
        batch_folder_layout.addWidget(self.batch_folder_btn)
        batch_folder_layout.addWidget(self.clear_batch_btn)
        
        # 源文件路徑區(qū)域
        source_file_layout = QHBoxLayout()
        source_file_layout.setSpacing(10)
        
        self.source_file_label = QLabel('文件路徑:')
        self.source_file_label.setFont(QFont("Microsoft YaHei", 10))
        self.source_file_label.setFixedWidth(100)
        self.source_file_label.setAlignment(Qt.AlignVCenter | Qt.AlignRight)
        
        self.source_file_edit = QLineEdit()
        self.setup_line_edit_bg(self.source_file_edit)
        self.source_file_edit.setFont(QFont("Microsoft YaHei", 10))
        self.source_file_edit.setText(r"D:\1.txt")
        
        self.source_file_btn = self.create_browse_button("瀏覽", self.select_source_file, self.BUTTON_BLUE)
        
        source_file_layout.addWidget(self.source_file_label)
        source_file_layout.addWidget(self.source_file_edit, 1)
        source_file_layout.addWidget(self.source_file_btn)
        
        # 功能按鈕區(qū)域
        button_layout = QHBoxLayout()
        button_layout.setSpacing(20)
        button_layout.setContentsMargins(0, 5, 0, 5)
        
        self.copy_button = QPushButton('開始批量復制')
        self.copy_button.setFont(QFont("Microsoft YaHei", 12, QFont.Bold))
        self.copy_button.setFixedSize(150, 40)
        self.copy_button.clicked.connect(self.start_copy)
        self.setup_button_style(self.copy_button, self.BUTTON_BLUE)
        
        self.advanced_button = QPushButton('高級功能')
        self.advanced_button.setFont(QFont("Microsoft YaHei", 12, QFont.Bold))
        self.advanced_button.setFixedSize(120, 40)
        self.advanced_button.clicked.connect(self.open_advanced)
        self.setup_button_style(self.advanced_button, self.BUTTON_GREEN)
        
        button_layout.addStretch(1)
        button_layout.addWidget(self.copy_button, alignment=Qt.AlignCenter)
        button_layout.addWidget(self.advanced_button, alignment=Qt.AlignCenter)
        button_layout.addStretch(1)
        
        # 日志顯示區(qū)域
        log_container = QVBoxLayout()
        log_container.setSpacing(5)
        
        self.log_label = QLabel('操作日志:')
        self.log_label.setFont(QFont("Microsoft YaHei", 10, QFont.Bold))
        
        self.log_text = QTextEdit()
        self.log_text.setReadOnly(True)
        self.log_text.setFont(QFont("Microsoft YaHei", 10))
        self.setup_line_edit_bg(self.log_text)
        
        log_container.addWidget(self.log_label)
        log_container.addWidget(self.log_text)
        
        # 添加所有組件到主布局
        main_layout.addLayout(batch_folder_layout)
        main_layout.addLayout(source_file_layout)
        main_layout.addLayout(button_layout)
        main_layout.addLayout(log_container, 1)
        
    def create_browse_button(self, text, callback, color):
        """創(chuàng)建統(tǒng)一風格的按鈕"""
        button = QPushButton(text)
        button.setFont(QFont("Microsoft YaHei", 10, QFont.Bold))
        button.setFixedWidth(80)
        button.clicked.connect(callback)
        self.setup_button_style(button, color)
        return button
        
    def setup_line_edit_bg(self, widget):
        """設置文本框背景顏色"""
        palette = widget.palette()
        palette.setColor(QPalette.Base, self.LINE_EDIT_BG)
        palette.setColor(QPalette.Text, QColor(0, 0, 0))
        widget.setPalette(palette)
        widget.setAutoFillBackground(True)
        
    def setup_button_style(self, button, base_color):
        """設置按鈕樣式"""
        button.setStyleSheet(f"""
            QPushButton {{
                background-color: rgb({base_color.red()}, {base_color.green()}, {base_color.blue()});
                color: white;
                border: none;
                padding: 5px;
                border-radius: 4px;
            }}
            QPushButton:hover {{
                background-color: rgb({int(base_color.red()*0.8)}, {int(base_color.green()*0.8)}, {int(base_color.blue()*0.8)});
            }}
            QPushButton:pressed {{
                background-color: rgb({int(base_color.red()*0.7)}, {int(base_color.green()*0.7)}, {int(base_color.blue()*0.7)});
            }}
        """)
        button.setFocusPolicy(Qt.NoFocus)

    # 文件夾選擇功能
    def select_batch_folders(self):
        """選擇多個文件夾并顯示路徑"""
        folder = QFileDialog.getExistingDirectory(self, "選擇文件夾", self.default_folder)
        if folder:
            if folder not in self.folder_list:
                self.folder_list.append(folder)
                # 顯示所有已選擇的文件夾路徑,用分號分隔
                self.batch_folder_edit.setText("; ".join(self.folder_list))
                QMessageBox.information(self, '成功', f'已添加 {len(self.folder_list)} 個文件夾')
    
    def clear_batch_folders(self):
        """清空已選擇的批量文件夾"""
        self.folder_list.clear()
        self.batch_folder_edit.clear()
        self.batch_folder_edit.setPlaceholderText("已選擇 0 個文件夾")
        QMessageBox.information(self, '提示', '已清空所有選擇的文件夾')

    # 文件選擇功能
    def select_source_file(self):
        file, _ = QFileDialog.getOpenFileName(self, "選擇源文件", os.path.dirname(self.source_file_edit.text()))
        if file:
            self.source_file_edit.setText(file)
        
    def start_copy(self):
        """批量復制邏輯"""
        source_file = self.source_file_edit.text().strip()
        
        # 驗證源文件
        if not source_file or not os.path.isfile(source_file):
            QMessageBox.warning(self, '文件錯誤', '請輸入有效的源文件路徑')
            return
            
        # 驗證批量文件夾
        if not self.folder_list:
            QMessageBox.warning(self, '路徑錯誤', '請選擇至少一個文件夾')
            return
            
        self.log_text.clear()
        try:
            # 遍歷所有目標文件夾
            for folder in self.folder_list:
                # 復制文件到每個目標文件夾的子文件夾中
                for root, dirs, files in os.walk(folder):
                    for dir in dirs:
                        dest_path = os.path.join(root, dir, os.path.basename(source_file))
                        try:
                            shutil.copy2(source_file, dest_path)
                            self.log_text.append(f"已復制到:{dest_path}")
                        except Exception as e:
                            self.log_text.append(f"復制失敗到 {dest_path}: {e}")
            
            QMessageBox.information(self, '完成', '批量復制操作已完成')
        except Exception as e:
            QMessageBox.critical(self, '錯誤', f'操作過程中發(fā)生錯誤: {str(e)}')
    
    def open_advanced(self):
        webbrowser.open("你自己的")

if __name__ == '__main__':
    app = QApplication(sys.argv)
    app.setStyle("Fusion")
    window = FileCopyTool()
    window.show()
    sys.exit(app.exec_())
    

源碼打包

1、你需要安裝Python(不會安裝的自行解決)

2、下載我提供的源碼文件

3、將源碼文件放在Python路徑下

4、打開cmd界面,輸入:“D:\Program Files (x86)\biancheng\python3113\python.exe” -m PyInstaller --onefile --windowed “D:\Program Files (x86)\biancheng\python3113\批量給文件夾添加文件v2.py”

其中D:\Program Files (x86)\biancheng\python3113要替換為你自己的Python路徑

到此這篇關于Python批量實現(xiàn)給文件夾添加文件的文章就介紹到這了,更多相關Python文件復制內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • python?print無法打印\r的問題及解決

    python?print無法打印\r的問題及解決

    這篇文章主要介紹了python?print無法打印\r的問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • django中的*args 與 **kwargs使用介紹

    django中的*args 與 **kwargs使用介紹

    這篇文章主要介紹了django中的*args 與 **kwargs使用介紹,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • Python YAML文件的讀寫操作詳解

    Python YAML文件的讀寫操作詳解

    這篇文章主要介紹了Python讀寫yaml文件,yaml 是專門用來寫配置文件的語言,非常簡潔和強大,之前用ini也能寫配置文件,有點類似于json格式,下面關于Python讀寫yaml文件的詳細資料,需要的小伙伴可以參考一下
    2022-08-08
  • Python列表插入元素到指定位置的5種高效技巧

    Python列表插入元素到指定位置的5種高效技巧

    本文主要介紹了Python列表插入元素到指定位置的5種高效方法,解決動態(tài)數(shù)據(jù)處理難題,涵蓋insert()、切片、extend()等多種技巧,適用于列表實時更新場景,感興趣的可以了解一下
    2026-04-04
  • 使用python批量生成insert語句的方法

    使用python批量生成insert語句的方法

    很多時候需要造數(shù)據(jù),大量的插入數(shù)據(jù),本文介紹了使用python批量生成insert語句的方法,需要的朋友們下面隨著小編來一起學習學習吧
    2021-05-05
  • 利用Python操作Word文檔頁碼的實際應用

    利用Python操作Word文檔頁碼的實際應用

    在撰寫長篇文檔時,經(jīng)常需要將文檔分成多個節(jié),每個節(jié)都需要單獨的頁碼,下面這篇文章主要介紹了利用Python操作Word文檔頁碼的相關資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2025-09-09
  • Python調用騰訊云短信服務發(fā)送手機短信

    Python調用騰訊云短信服務發(fā)送手機短信

    這篇文章主要為大家介紹了Python調用騰訊云短信服務發(fā)送手機短信,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-05-05
  • 最強Python可視化繪圖庫Plotly詳解用法

    最強Python可視化繪圖庫Plotly詳解用法

    數(shù)據(jù)分析離不開數(shù)據(jù)可視化。Plotly 是一款用來做數(shù)據(jù)分析和可視化的在線平臺,功能非常強大,可以在線繪制很多圖形比如條形圖、散點圖、餅圖、直方圖等等
    2021-11-11
  • Python Logging庫完全指南

    Python Logging庫完全指南

    本文主要介紹了Python Logging庫完全指南,包括為什么使用Logging、快速開始、核心配置、進階技巧和實用場景示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2026-02-02
  • python基于plotly實現(xiàn)畫餅狀圖代碼實例

    python基于plotly實現(xiàn)畫餅狀圖代碼實例

    這篇文章主要介紹了python基于plotly實現(xiàn)畫餅狀圖代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-12-12

最新評論

吉安县| 张家川| 札达县| 固阳县| 横山县| 安多县| 大厂| 丰原市| 临海市| 福泉市| 灌云县| 松滋市| 中超| 江门市| 小金县| 思南县| 延安市| 德江县| 塔城市| 特克斯县| 东港市| 赣榆县| 鸡泽县| 缙云县| 高平市| 渭南市| 南部县| 广丰县| 五大连池市| 娱乐| 嘉义市| 孝昌县| 区。| 河曲县| 平度市| 连城县| 建昌县| 古蔺县| 南投市| 文安县| 胶南市|