2016-09-29 77 views
2

我有一个名为的文件cookies.txtPyQt5:无法使用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语法。

回答

0

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

要写入非资源路径:

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() 
+0

哦,我不知道:/表示资源路径。谢谢 :) – wrufesh

1

QTextStream(cookie.toRawForm(1), fd.open(QIODevice.WriteOnly)),传递2个参数,一个QByteArrayboolQIODevice::open返回一个布尔值),但QTextStream不能采取QByteArraybool