python - Python Paramiko SFTP 打开文件从 N 行开始
问题描述
因为我在服务器中的文件很大,为了节省从服务器下载文件的时间,所以我使用 paramikosftp.open(file)
来读取服务器中的文件。
有什么方法可以让我们从文件的任何行使用sftp.open(file)
或其他方式开始读取文件?
这就是我过去从本地目录读取文件的方式
# open the file and start reading from the nth line
for line in itertools.islice(file, no_line, None):
print(line)
这就是我从服务器读取文件的方式
ssh = paramiko.SSHClient()
ssh.connect(host, port, username, password)
readfile = ssh.open_sftp()
file = readfile.open(os.path.join(path, file))
for line in file:
print(line)
解决方案
SFTP 中没有允许您跳到第 N 行的 API。
SFTPFile.seek
您可以使用方法从第 N 个字节开始读取:
file = readfile.open(os.path.join(path, file))
file.seek(offset)
for line in file:
print(line)
推荐阅读
- java - Math.random() 函数在 for 循环中不起作用
- ndepend - 计算 NDepend 中间接使用的程序集的数量
- python - 用python解析表达式:从字符串到整数
- angular - 如何在角度 4 中绘制箭头?
- python - 美化终端中的 JSON 输出 - Visual Studio Code - Python
- java - 在 Spring -boot 中使用 ForEach 进行迭代
- laravel - Laravel api路由不起作用,它总是重定向
- laravel - 如何使用 nexmo 应用内语音进行多个通话?
- qt - 无法在通过 UART 连接到 Zigbee 的 2 个 Raspberry 之间发送和接收图像
- coq - 改进依赖类型的反向函数