weixin_55061897 2022-04-18 08:22 采纳率: 60%
浏览 44
已结题

python中如何检测刚生成的文件

想试着写一个类似idle的程序:
源码如下:

import sys
import mainui
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog,QTextBrowser
import subprocess
import win32api
# reply=subprocess.getstatusoutput(cmd="cmd")
# print(reply)


class MainCode(QMainWindow, mainui.Ui_MainWindow):

    def __init__(self):
        QMainWindow.__init__(self)
        mainui.Ui_MainWindow.__init__(self)
        self.setupUi(self)
        self.pushButton_2.clicked.connect(self.on_save)
        self.pushButton.clicked.connect(self.on_open)
        self.pushButton_3.clicked.connect(self.open_py)

    def on_save(self):
        FullFileName, _ = QFileDialog.getSaveFileName(self, '文件另存为', r'./', 'TXT (*.txt)')
        if not FullFileName:
            return
        set_text = self.textEdit.toPlainText()
        with open(FullFileName, 'wt') as fp:
            print(set_text, file=fp)
    def open_py(self):
        win32api.ShellExecute(0, 'open', 'python.exe', FullFileName, '', 1)

    def on_open(self):
        txtstr = ""
        FullFileName, _ = QFileDialog.getOpenFileName(self, '打开', r'./', 'TXT (*.txt)')
        if not FullFileName:
            return
        with open(FullFileName, 'rt') as fp:
            lines = fp.readlines()
            for line in lines:
                txtstr = txtstr + line
        self.textEdit.setText(txtstr)
    # def connect_cmd(self):
    #     reply = subprocess.getstatusoutput("cmd")
    #     print(reply)
    # def show_cmd(self):
    #     QTextBrowser.setText()



if __name__ == '__main__':
    app = QApplication(sys.argv)
    ui = MainCode()
    ui.show()
    sys.exit(app.exec())


mainui.py:


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

# Form implementation generated from reading ui file 'mainui.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# 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_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1232, 594)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(10, 10, 121, 31))
        self.label.setObjectName("label")
        self.layoutWidget = QtWidgets.QWidget(self.centralwidget)
        self.layoutWidget.setGeometry(QtCore.QRect(10, 40, 591, 501))
        self.layoutWidget.setObjectName("layoutWidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.layoutWidget)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setObjectName("verticalLayout")
        self.textEdit = QtWidgets.QTextEdit(self.layoutWidget)
        self.textEdit.setObjectName("textEdit")
        self.verticalLayout.addWidget(self.textEdit)
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.pushButton = QtWidgets.QPushButton(self.layoutWidget)
        self.pushButton.setObjectName("pushButton")
        self.horizontalLayout.addWidget(self.pushButton)
        self.pushButton_2 = QtWidgets.QPushButton(self.layoutWidget)
        self.pushButton_2.setObjectName("pushButton_2")
        self.horizontalLayout.addWidget(self.pushButton_2)
        self.verticalLayout.addLayout(self.horizontalLayout)
        self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_3.setGeometry(QtCore.QRect(630, 40, 131, 41))
        self.pushButton_3.setObjectName("pushButton_3")
        self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget)
        self.textBrowser.setGeometry(QtCore.QRect(630, 120, 461, 391))
        self.textBrowser.setObjectName("textBrowser")
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(630, 100, 54, 12))
        self.label_2.setObjectName("label_2")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 1232, 23))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.label.setText(_translate("MainWindow", "请在此输入:"))
        self.pushButton.setText(_translate("MainWindow", "打开文件"))
        self.pushButton_2.setText(_translate("MainWindow", "保存文件"))
        self.pushButton_3.setText(_translate("MainWindow", "运行文件"))
        self.label_2.setText(_translate("MainWindow", "程序日志"))

想达到的效果:
检测到刚保存的文件,并用根目录下的python,exe来运行此程序,然后在右边的TextBrowser组件中显示事实日志
显示实时日志我可以自己百度,但是“检测到刚保存的文件,并用根目录下的python,exe来运行此程序”这里不是很会写

  • 写回答

1条回答 默认 最新

  • 陈年椰子 2022-04-18 08:37
    关注

    不知道下面的描述是否是你想要实现的逻辑。
    刚保存的文件, 不就是你的另存为 或者打开文件的名字咯。
    有了文件名,那就可以建立命令了。
    直接调用os.system

    os.system("python.exe youfile.py")

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 4月26日
  • 已采纳回答 4月18日
  • 创建了问题 4月18日

悬赏问题

  • ¥15 如何构建全国统一的物流管理平台?
  • ¥100 ijkplayer使用AndroidStudio/CMake编译,如何支持 rtsp 直播流?
  • ¥20 和学习数据的传参方式,选择正确的传参方式有关
  • ¥15 这是网络安全里面的poem code
  • ¥15 用js遍历数据并对非空元素添加css样式
  • ¥15 使用autodl云训练,希望有直接运行的代码(关键词-数据集)
  • ¥50 python写segy数据出错
  • ¥20 关于线性结构的问题:希望能从头到尾完整地帮我改一下,困扰我很久了
  • ¥30 3D多模态医疗数据集-视觉问答
  • ¥20 设计一个二极管稳压值检测电路