我有一个名为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
和bool
(QIODevice::open
返回布尔值),但QTextStream
不能接受QByteArray
和bool
。
您真的要写入资源路径吗?资源是只读的,所以这是行不通的。
写入非资源路径:
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()