1. 程式人生 > 其它 >python獲取檔案子目錄

python獲取檔案子目錄

技術標籤:pythonpython

本次復現的是:使用python實現獲取資料夾的子目錄,並儲存到txt檔案
設計要求:需要帶qt介面顯示,給出資料夾路徑能夠自動獲取子目錄,並儲存到本地資料夾

pyqt5安裝:pyqt5與flask安裝

1.pyqt介面設計

將程式碼儲存為untitled.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.15.1
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing. from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Dialog(object): def setupUi(self, Dialog): Dialog.setObjectName("Dialog") Dialog.resize(371, 173) self.pushButton = QtWidgets.QPushButton(
Dialog) self.pushButton.setGeometry(QtCore.QRect(280, 70, 75, 23)) self.pushButton.setObjectName("pushButton") self.lineEdit = QtWidgets.QLineEdit(Dialog) self.lineEdit.setGeometry(QtCore.QRect(30, 70, 231, 20)) self.lineEdit.setObjectName("lineEdit"
) self.label = QtWidgets.QLabel(Dialog) self.label.setGeometry(QtCore.QRect(30, 30, 101, 16)) self.label.setObjectName("label") self.label_5 = QtWidgets.QLabel(Dialog) self.label_5.setGeometry(QtCore.QRect(250, 150, 181, 16)) self.label_5.setObjectName("label_5") self.widget = QtWidgets.QWidget(Dialog) self.widget.setGeometry(QtCore.QRect(40, 100, 194, 50)) self.widget.setObjectName("widget") self.verticalLayout = QtWidgets.QVBoxLayout(self.widget) self.verticalLayout.setContentsMargins(0, 0, 0, 0) self.verticalLayout.setObjectName("verticalLayout") self.label_2 = QtWidgets.QLabel(self.widget) self.label_2.setObjectName("label_2") self.verticalLayout.addWidget(self.label_2) self.label_3 = QtWidgets.QLabel(self.widget) self.label_3.setObjectName("label_3") self.verticalLayout.addWidget(self.label_3) self.label_4 = QtWidgets.QLabel(self.widget) self.label_4.setObjectName("label_4") self.verticalLayout.addWidget(self.label_4) self.retranslateUi(Dialog) QtCore.QMetaObject.connectSlotsByName(Dialog) def retranslateUi(self, Dialog): _translate = QtCore.QCoreApplication.translate Dialog.setWindowTitle(_translate("Dialog", "獲取子目錄指令碼程式")) self.pushButton.setText(_translate("Dialog", "執行")) self.label.setText(_translate("Dialog", "請輸入檔名地址:")) self.label_5.setText(_translate("Dialog", "2020.10.27 DYW編輯")) self.label_2.setText(_translate("Dialog", "說明:")) self.label_3.setText(_translate("Dialog", "將地址複製到文字框中,點選執行")) self.label_4.setText(_translate("Dialog", "即可以看到當前資料夾有子資料夾名"))

2.驅動程式設計

# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
import os
import sys
from lib2to3.pytree import convert

from PyQt5.QtWidgets import QApplication, QMainWindow
from functools import partial
import untitled


dirList = []

def findDir_BaseFunction(pathname):
    global dirList
    if os.path.isdir(pathname):
        files = os.listdir(pathname)
        if (len(files)) > 0:
            for i in files:
                path = str(pathname) + "/" + str(i)
                print(path)
                with open("子目錄資料夾.txt", "a") as f:
                    f.write(path + "     \n")  # 自帶檔案關閉功能,不需要再寫f.close()
                if os.path.isdir(path):

                    #with open("子目錄資料夾.txt", "a") as f:
                        #f.write(i + "      " + str(s) +"\n")
                        # 自帶檔案關閉功能,不需要再寫f.close()

                    dirList.append(i)
                    findDir_BaseFunction(path)
        else:
            dirList.append(pathname)
            return dirList
    else:
        print("1")
def findDir(pathname):
    if os.path.isdir(pathname):
        print(pathname)
    files = os.listdir(pathname)
    if (len(files)) > 0:
        dirList.append(pathname)
    findDir_BaseFunction(pathname)


def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.


# Press the green button in the gutter to run the script.
def click_success():
    inp = ui.lineEdit.text()
    findDir(str(inp))

if __name__ == '__main__':
    app = QApplication(sys.argv)
    MainWindow = QMainWindow()
    ui = untitled.Ui_Dialog()
    ui.setupUi(MainWindow)
    MainWindow.show()
    ui.pushButton.clicked.connect(click_success)
    #input = ui.textBrowser.text()
    #ui.pushButton.clicked.connect(partial(convert, ui))
    sys.exit(app.exec_())
# See PyCharm help at https://www.jetbrains.com/help/pycharm/

3.執行結果
給出路徑後,點選執行即可看見當前資料夾有,子目錄資料夾.txt
另外程式需要進行打包
打包方法:

pip install pyinstaller
pyinstaller -F main.py

進入到當前目錄或者在pycharm中開啟終端,執行上面的命令即可在當前資料夾的dist資料夾下看到exe檔案
在這裡插入圖片描述
到此,這個小工具就開發完成了