PyQt5:不能使用QFile将cookie写入文件



我有一个名为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个参数,QByteArraybool (QIODevice::open返回布尔值),但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()

相关内容

  • 没有找到相关文章

最新更新