Python 文件拷贝器 Python PyQt5实战项目之文件拷贝器的具体实现详解

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Python 文件拷贝器 Python PyQt5实战项目之文件拷贝器的具体实现详解

不侠居   2021-11-09 我要评论
想了解Python PyQt5实战项目之文件拷贝器的具体实现详解的相关内容吗,不侠居在本文为您仔细讲解Python 文件拷贝器的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Python,文件拷贝器,PyQt5,文件拷贝器,下面大家一起来学习吧。

简介

写了一个简单的文件夹内容下所有文件复制到另一个文件夹内,主要逻辑代码是来自《2小时玩转python多线程编程》中的一个章节。

UI设置

def ui_init(self):
        '''
        界面的函数
        '''
        self.setWindowTitle('拷贝器')
        self.resize(600,400)
        self.setMinimumSize(600,400) # 设置窗口的最小值

        '''控件'''
        self.root_btn = QPushButton()
        self.copy_btn = QPushButton()
        self.start_btn = QPushButton()
        self.root_text = QTextBrowser()
        self.copy_text = QTextBrowser()
        self.log = QTextBrowser()
        self.h1_layout = QHBoxLayout()
        self.h2_layout = QHBoxLayout()
        self.h3_layout = QHBoxLayout()
        self.v_layout = QVBoxLayout()
        self.progerss =QProgressBar()
        self.finish_sound = QSound(':resource/finish.wav') # 设置提示音

        '''控件设置'''
        self.root_btn.setText('选择文件夹')
        self.root_btn.setFixedSize(150,30)
        self.copy_btn.setText('选择拷贝路径')
        self.copy_btn.setFixedSize(150,30) 
        self.start_btn.setText('开始')
        self.start_btn.setFixedSize(50,30)
        self.root_text.setFixedHeight(27)
        self.copy_text.setFixedHeight(27)
        self.progerss.setValue(0)

        '''控件摆放'''
        self.h1_layout.addWidget(self.root_text)
        self.h1_layout.addWidget(self.root_btn)
        self.h2_layout.addWidget(self.copy_text)
        self.h2_layout.addWidget(self.copy_btn)
        self.h3_layout.addWidget(self.progerss)
        self.h3_layout.addWidget(self.start_btn)
        self.v_layout.addLayout(self.h1_layout)
        self.v_layout.addLayout(self.h2_layout)
        self.v_layout.addWidget(self.log)
        self.v_layout.addLayout(self.h3_layout)
        self.setLayout(self.v_layout)

这次加入了一个完成的音效

  • QSound解析文件时,可能会出现这问题QSoundEffect(qaudio): Error decoding source

self.finish_sound = QSound('resource/finish.wav') # 设置提示音 原来这这样写的,但会出现上面的问题,就在写一个qrc文件,再将qrc文件转成py文件,再引入这个py文件,这样就可以使用了。在使用这个音频只需要在路径上加一个 : ,就如这样self.finish_sound = QSound(':resource/finish.wav') # 设置提示音

  • qrc文件转py文件

先新建一个txt文件,在向里面写入这样的语句:

<RCC>
	<qresource prefix ="resource/">
		<file alias="finish.wav">resource/finish.wav</file>
	</qresource>
</RCC>

resource/是放音频的文件夹名
finish.wav是音频名
resource/finish.wav是完整音频路径
接着将文件后缀改为qrc,在利用cmd命令窗中键入pyrcc5 -o resource.qrc resource.py,将.qrc文件转成.py文件。

主要逻辑

def variates_init(self):
        '''
        储存变量的函数
        '''
        self.root_path = '' # 要拷贝的路径
        self.copy_path = '' # 要拷贝到的路径
        self.file_list = [] # 文件名集合
        self.len = 0 # 文件夹下文件数量

def copy_file(self):
        '''
        拷贝文件的函数
        '''
        count = 0 # 临时设置进度条数值
        self.progerss.setRange(0,self.len) # 设置进度条的数值
        self.progerss.setValue(0) # 设置进度条初始值

        '''拷贝器主逻辑'''
        for file in self.file_list:
            root_path = self.root_path + "/" + file
            copy_path = self.copy_path + "/" + file

            with open(root_path, "rb") as root_file:
                with open(copy_path, "wb") as copy_file:
                    while True:
                        data = root_file.read(1024)
                        if data:
                            copy_file.write(data)
                        else:
                            count += 1 
                            self.progerss.setValue(count)
                            break


    def dir_file(self):
        '''
        遍历目录的函数
        '''
        filelist = os.listdir(self.root_path)
        self.file_list = filelist

    def len_file(self):
        '''
        文件数量的函数
        '''
        self.len=len(self.file_list)

拷贝器的逻辑:

  • 从文件名集合中获取文件名
  • 合并出原始文件路径和拷贝到的路径
  • 根据原始文件路径打开文件模式为只读,根据拷贝到的路径新建一个文件写入
  • 拷贝的文件每次写入1024字节,当没有数据后,就结束写入并保存文件,进度条数值加1

信号与槽

def connect_init(self):
        '''
        信号与槽连接的函数
        '''
        self.root_btn.clicked.connect(lambda:self.btn_slot())
        self.copy_btn.clicked.connect(lambda:self.btn_slot())
        self.start_btn.clicked.connect(self.start_slot)

    def start_slot(self):
        '''
        开始按键的槽函数
        '''
        self.root_btn.setEnabled(False)
        self.copy_btn.setEnabled(False)
        self.start_btn.setEnabled(False)
        self.dir_file() # 遍历指定文件夹下的文件并添加到self.file_list集合中
        self.len_file() # 获取文件夹下文件数量
        self.copy_file() # 开始拷贝文件
        self.log.append('拷贝成功!')
        self.finish_sound.play() # 播放完成后的提示音

    def btn_slot(self):
        '''
        上面两个按键的槽函数
        '''
        btn = self.sender() 
        if btn == self.root_btn:
            directory = QFileDialog.getExistingDirectory(None,"选取文件夹","C:/")
            if directory:
                self.root_text.setText(directory)
                self.root_path = directory
                self.log.append('选择文件成功!')
        elif btn == self.copy_btn:
            directory = QFileDialog.getExistingDirectory(None,"选取拷贝位置","C:/")
            if directory:
                self.copy_text.setText(directory)
                self.copy_path = directory
                self.log.append('选取拷贝位置成功!')

成果展示

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们