PyQt5:无法使用 QFile 将 cookie 写入文件

PyQt5: cannot write cookie to file using QFile

我有一个名为 cookies.txt.

的文件
fd = QFile(":/cookies.txt")
available_cookies = QtNetwork.QNetworkCookieJar().allCookies()
for cookie in available_cookies:
   print(cookie.toRawForm(1))
   QTextStream(cookie.toRawForm(1), fd.open(QIODevice.WriteOnly))
fd.close()

这是我的完整回溯:

QTextStream(cookie.toRawForm(1),        fd.open(QIODevice.WriteOnly))
TypeError: arguments did not match any overloaded call:
QTextStream(): too many arguments
QTextStream(QIODevice): argument 1 has unexpected type 'QByteArray'
QTextStream(QByteArray, mode: Union[QIODevice.OpenMode,    QIODevice.OpenModeFlag] = QIODevice.ReadWrite): argument 2 has unexpected type 'bool'

我正在遵循 C++ 文档,但在编写相应的 python 语法时遇到问题。

QTextStream(cookie.toRawForm(1), fd.open(QIODevice.WriteOnly)) 中,您传递了 2 个参数,一个 QByteArray 和一个 boolQIODevice::open returns 一个布尔值),但是 QTextStream 不能用 QByteArraybool.

您真的要写入资源路径吗?资源是只读的,所以这是行不通的。

写入非资源路径:

fd = QFile('/tmp/cookies.txt')
if fd.open(QIODevice.WriteOnly):
    available_cookies = QtNetwork.QNetworkCookieJar().allCookies()
    stream = QTextStream(fd)
    for cookie in available_cookies:
        data = cookie.toRawForm(QtNetwork.QNetworkCookie.Full)
        stream << data
    fd.close()