我希望用户打开文件,文件名传递给使用该文件保存一些数据的函数。我为此使用PyQt5 getOpenFileName:
fileName, _ = QFileDialog.getOpenFileName(None, "Open Data File")
但是,我收到此错误消息:“进程完成,退出代码-1073740791(0xC0000409)”。我正在使用python 3.7.1和pyqt 5.9.2。我的问题是如何解决此错误。
编辑: 这是示例代码:
import pandas as pd
import csv
from PyQt5.QtWidgets import QFileDialog
import os
def saveSubjectList(FileNamePath,subjectList):
columns = list(subjectList[0].__dict__.keys())
vals = []
for iSub in range(len(subjectList)):
valsSub = subjectList[iSub].__dict__.values()
vals.append(valsSub)
subject_pd = pd.DataFrame(vals, columns=columns)
subject_pd.to_csv(FileNamePath)
def loadSubjectList(FileNamePath):
csvFile = open(FileNamePath, 'r')
csvFileArray = []
for row in csv.reader(csvFile):
csvFileArray.append(row[1:])
keys = csvFileArray[0]
subList = []
for sub in csvFileArray[1:]:
subject1 = Subject(sub[0])
for val, key in zip(sub, keys):
setattr(subject1, key, val)
subList.append(subject1)
for item in range(len(subList)):
print(subList[item].__dict__)
class Subject:
def __init__(self, Id):
self.Id = Id
self.Name = ""
self.FrameNumberList = []
self.Color = [0, 1, 0]
if __name__ == '__main__':
subjectList = []
for i in range(2):
subject1 = Subject(i)
subject1.Name = "Subject" + str(i)
subject1.FrameNumberList = [i, 10, 23, 23]
subjectList.append(subject1)
FileNamePath = os.getcwd()
fileName = 'New'
fileNameSuffix = 'csv'
csvPathAndName = os.path.join(FileNamePath, fileName + '.' +
fileNameSuffix)
saveSubjectList(csvPathAndName, subjectList)
loadSubjectList(csvPathAndName)
这是没有getOpenFileName的初始代码,可以正常工作。然后,我将行从FileNamePath = os.getcwd删除到csvPathAndName = ...,并将其替换为:
csvPathAndName, _ = QFileDialog.getOpenFileName(None, "Open Data File")
因为我希望用户能够在计算机中的任何位置创建或选择文件,但随后出现错误。