PyQt5:无法使用QFile将cookie写入文件
问题描述:
我有一个名为的文件cookies.txt。PyQt5:无法使用QFile将cookie写入文件
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语法。
答
你真的想写入资源路径吗?资源是只读的,所以这是行不通的。
要写入非资源路径:
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()
答
在QTextStream(cookie.toRawForm(1), fd.open(QIODevice.WriteOnly))
,传递2个参数,一个QByteArray
和bool
(QIODevice::open
返回一个布尔值),但QTextStream
不能采取QByteArray
用bool
。
哦,我不知道:/表示资源路径。谢谢 :) – wrufesh