2024-05-18 14:29:44 发布
网友
我想用fabric获取远程文件的内容,而不需要创建临时文件。
from StringIO import StringIO from fabric.api import get fd = StringIO() get(remote_path, fd) content=fd.getvalue()
import tempfile from fabric.api import get with tempfile.TemporaryFile() as fd: get(remote_path, fd) fd.seek(0) content=fd.read()
见:http://docs.python.org/2/library/tempfile.html#tempfile.TemporaryFile
和:http://docs.fabfile.org/en/latest/api/core/operations.html#fabric.operations.get
使用Python 3(和fabric3)时,我在使用io.StringIO:string argument expected, got 'bytes'时遇到这个致命错误,显然是因为Paramiko用字节写入类似文件的对象。所以我改为使用io.BytesIO,它工作了:
io.StringIO
string argument expected, got 'bytes'
io.BytesIO
from io import BytesIO def _read_file(file_path, encoding='utf-8'): io_obj = BytesIO() get(file_path, io_obj) return io_obj.getvalue().decode(encoding)
见:http://docs.python.org/2/library/tempfile.html#tempfile.TemporaryFile
和:http://docs.fabfile.org/en/latest/api/core/operations.html#fabric.operations.get
使用Python 3(和fabric3)时,我在使用
io.StringIO
:string argument expected, got 'bytes'
时遇到这个致命错误,显然是因为Paramiko用字节写入类似文件的对象。所以我改为使用io.BytesIO
,它工作了:相关问题 更多 >
编程相关推荐