[英]Python: OSError: [Errno 9] Bad file descriptor occurs while trying to write to csv
I'm a beginner in programming and I tried all my best to solve this error by my own.我是编程初学者,我尽力自己解决这个错误。 In the Code below I try to write something to a.csv file with only writing the name of the file once (in Line 7), to make it a more sustainable code.
在下面的代码中,我尝试将一些内容写入 a.csv 文件,只写一次文件名(在第 7 行),以使其成为更可持续的代码。
But with the code below I get an "Bad file descriptor occurs" error.但是使用下面的代码我得到一个“发生错误的文件描述符”错误。 I already figured out that the error occurs because probably the.csv file gets closed too early.
我已经发现发生错误的原因可能是 .csv 文件过早关闭。 I don't know why because I don't call close() anywhere.
我不知道为什么,因为我没有在任何地方调用 close() 。 As a workaround I can change the variable data to "NewFile.csv" everywhere in the code.
作为一种解决方法,我可以将代码中各处的变量数据更改为“NewFile.csv”。
I really would appreciate a helping hand here to improve my programming.我真的很感激能帮我改进我的编程。
Thanks for your help!谢谢你的帮助!
import sys
import csv
from qtpy import QtWidgets
from UI.mainwindow import Ui_MainWindow
#Dateiname/-pfad an Variable vergeben
data = "NewFile.csv"
app = QtWidgets.QApplication(sys.argv)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent = None, data=data):
super().__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.setWindowTitle("Studentenverwaltung")
self.ui.NewButton.clicked.connect(self.onNewButtonClick)
self.loadTable(data)
self.ui.SaveButton.clicked.connect(self.onSaveButtonClick)
def loadTable(self, filename):
self.ui.tableWidget.setRowCount(0)
with open(filename, "r", newline="") as file:
filereader = csv.reader(file, delimiter=";", quotechar='"')
for row in filereader:
first = row[0]
last = row[1]
subject = row[2]
print(first + " " + last)
newRow = self.ui.tableWidget.rowCount()
self.ui.tableWidget.insertRow(newRow)
self.ui.tableWidget.setItem(newRow, 0, QtWidgets.QTableWidgetItem(first))
self.ui.tableWidget.setItem(newRow, 1, QtWidgets.QTableWidgetItem(last))
self.ui.tableWidget.setItem(newRow, 2, QtWidgets.QTableWidgetItem(subject))
def onSaveButtonClick(self, data):
with open(data, "w", encoding="utf-8") as file:
filewriter = csv.writer(file, delimiter=";", quotechar='"')
totalRows = self.ui.tableWidget.rowCount()
totalColumns = self.ui.tableWidget.columnCount()
for row in range(totalRows):
rowdata = []
for column in range(totalColumns):
item = self.ui.tableWidget.item(row, column)
if item is not None:
rowdata.append(item.text())
else:
rowdata.append('')
filewriter.writerow(rowdata)
print(rowdata)
def onNewButtonClick(self):
newRow = self.ui.tableWidget.rowCount()
self.ui.tableWidget.insertRow(newRow)
self.ui.tableWidget.setItem(newRow, 0, QtWidgets.QTableWidgetItem(""))
self.ui.tableWidget.setItem(newRow, 1, QtWidgets.QTableWidgetItem(""))
self.ui.tableWidget.setItem(newRow, 2, QtWidgets.QTableWidgetItem(""))
# Cursor auf neue Zelle setzen
editingCell = self.ui.tableWidget.item(newRow, 0)
self.ui.tableWidget.editItem(editingCell)
window = MainWindow()
window.show()
sys.exit(app.exec_())
Error Message:错误信息:
OSError: [Errno 9] Bad file descriptor
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/User/Desktop/Coding/Python/PyQt/main.py", line 57, in onSaveButtonClick
print(rowdata)
OSError: [Errno 9] Bad file descriptor
Process finished with exit code 134 (interrupted by signal 6: SIGABRT)
Remove the data
parameter from the parameter list of onSaveButtonClick
.从
onSaveButtonClick
的参数列表中删除data
参数。
def onSaveButtonClick(self):
When the function is called by Qt, it doesn't pass the filename as an argument.当 function 被 Qt 调用时,它不会将文件名作为参数传递。 You want to use the global variable for that, and declaring a parameter shadows that.
你想为此使用全局变量,并声明一个参数来隐藏它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.