接口类方法没有将文件保存到选择的文件夹

问题描述 投票:0回答:1

将文件保存到指定路径时出现问题。这是直到最后才执行的代码的一部分(即它从服务器收到响应,然后静默,尽管应该形成一个数据帧以便随后保存到文件中):

# accepting the server response
    def recv_msg(self):
        data = self.sock.recv(10240)
        data = pickle.loads(data, encoding='utf-8')
        rep_file = data.to_csv('report_df.csv', index=False)
        target_fld = self.get_folder()
        os.path.join(str(target_fld), rep_file)
        print('file upload')
        self.sock.close()

这是模块中的所有代码:

import sys
import os
import socket
import pickle
from PyQt6 import QtWidgets
from PyQt6.QtWidgets import QApplication
from upload_client_gui import Ui_MainWindow

class gui(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.connect(('localhost', 9090))

        self.download_folder = None
        self.ui.pushButton.clicked.connect(self.get_folder)
        self.ui.pushButton_2.clicked.connect(self.send_msg)

    # selecting the date of the report to be uploaded
    def calendar_date(self):
        date = self.ui.calendarWidget.selectedDate()
        string_date = str(date.toPyDate())
        return string_date

    # the file explorer opens to select the folder where the file will be saved
    def get_folder(self):
        try:
            self.download_folder = QtWidgets.QFileDialog.getExistingDirectory(self, 'Select a folder to save')
            os.chdir(self.download_folder)
            print(self.download_folder)
        except:
            pass

    # sending a date to the server via a socket
    def send_msg(self):
        msg = self.calendar_date()
        print(msg)
        self.sock.send(msg.encode('utf-8'))

    # accepting the server response
    def recv_msg(self):
        data = self.sock.recv(10240)
        data = pickle.loads(data, encoding='utf-8')
        rep_file = data.to_csv('report_df.csv', index=False)
        target_fld = self.get_folder()
        os.path.join(str(target_fld), rep_file)
        print('file upload')
        self.sock.close()

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

文件夹选择有效,返回绝对路径,套接字请求和响应也有。

请帮我看看我的问题出在哪里。

在没有GUI的版本中,socket可以正常工作

python sockets pyqt
1个回答
0
投票

我发现了我的错误!有必要将发送和接收消息放在一个方法中,并稍微重写所选文件夹中文件的创建。我希望它对某人有用:

import sys
import os
import socket
import pickle
from PyQt6 import QtWidgets
from PyQt6.QtWidgets import QApplication
from upload_client_gui import Ui_MainWindow

class Gui(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.connect(('localhost', 9090))

        self.download_folder = None
        self.ui.pushButton.clicked.connect(self.get_folder)
        self.ui.pushButton_2.clicked.connect(self.send_msg)

    # selecting the date of the report to be uploaded
    def calendar_date(self):
        date = self.ui.calendarWidget.selectedDate()
        string_date = str(date.toPyDate())
        return string_date

    # the file explorer opens to select the folder where the file will be saved
    def get_folder(self):
        try:
            self.download_folder = QtWidgets.QFileDialog.getExistingDirectory(self, 'Select a folder to save')
            self.download_folder = self.download_folder.replace('/', '\\') + '\\'
        except:
            pass

    # sending the date to the server and receiving information from the server via a socket
    def send_msg(self):
        msg = self.calendar_date()
        self.sock.send(msg.encode('utf-8'))

        data = self.sock.recv(10240)
        data = pickle.loads(data, encoding='utf-8')
        try:
            target_fld = self.download_folder
            data.to_csv(os.path.join(str(target_fld), 'report_df.csv'), index=False)
            self.sock.close()
        except Exception as e:
            print(e)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = Gui()
    win.show()
    sys.exit(app.exec())
© www.soinside.com 2019 - 2024. All rights reserved.