使用 Paramiko 更改 SFTP 服务器上的目录
Changing directory on SFTP server with Paramiko
我在开发中成功 运行 这个脚本,但在生产中文件结构略有不同,我收到 'FileNotFoundError: Errno 2 error No such file' 错误代码。
当我使用 root 登录时,我相信我登陆了这个目录:
/root
我需要工作的目录是:
/BackupStorage/Test/Here
我认为我遇到了错误,因为我的会话正在尝试从 /root
目录 运行。如何将目录更改为 /BackupStorage/Test/Here
?还是我完全错了?
local_path = r'C:\Data\Scripts\Trial\Test\Here'
remote_path = f'/BackupStorage/Test/Here'
hostname = 'hostname1'
password = 'password123'
username = 'root'
port = 22
print(f'Connecting to {hostname} ...')
session = paramiko.Transport((hostname, 22))
session.connect(username=username, password=password)
sftp = paramiko.SFTPClient.from_transport(session)
print(f'Connected to {hostname} - Remote Session Opened')
latest_time = -1
latest = None
# Below for loop finds latest sub-directory
for file_attr in sftp.listdir_attr(path=remote_path):
if stat.S_ISDIR(file_attr.st_mode) and file_attr.st_mtime > latest_time:
latest_time = file_attr.st_mtime
latest = file_attr.filename
# Below for loop finds the .zip file within latest sub-directory
for file_attr in sftp.listdir_attr(path=f'{remote_path}/{latest}'):
if file_attr.filename.endswith('.zip'):
sftp.get(
f'{remote_path}/{latest}/{file_attr.filename}',
f'{local_path}\{file_attr.filename}'
)
print(f'Most recent {branch_name} file successfully retrieved.')
session.close
print('Remote Session Closed.')
感谢您的帮助,非常感谢。
使用SFTPClient.chdir
:
sftp.chdir(path)
或者干脆使用绝对路径或正确的相对路径到家。
糟糕的是,我花了好几个小时研究这个问题,而我做错的只是将一个元组缩减为一个对象……后面没有逗号。这意味着当我遍历我认为是一个元组的东西时,我的代码正在遍历一个字符串——导致我的目标路径不完整。谢谢大家的回复。
我在开发中成功 运行 这个脚本,但在生产中文件结构略有不同,我收到 'FileNotFoundError: Errno 2 error No such file' 错误代码。
当我使用 root 登录时,我相信我登陆了这个目录:
/root
我需要工作的目录是:
/BackupStorage/Test/Here
我认为我遇到了错误,因为我的会话正在尝试从 /root
目录 运行。如何将目录更改为 /BackupStorage/Test/Here
?还是我完全错了?
local_path = r'C:\Data\Scripts\Trial\Test\Here'
remote_path = f'/BackupStorage/Test/Here'
hostname = 'hostname1'
password = 'password123'
username = 'root'
port = 22
print(f'Connecting to {hostname} ...')
session = paramiko.Transport((hostname, 22))
session.connect(username=username, password=password)
sftp = paramiko.SFTPClient.from_transport(session)
print(f'Connected to {hostname} - Remote Session Opened')
latest_time = -1
latest = None
# Below for loop finds latest sub-directory
for file_attr in sftp.listdir_attr(path=remote_path):
if stat.S_ISDIR(file_attr.st_mode) and file_attr.st_mtime > latest_time:
latest_time = file_attr.st_mtime
latest = file_attr.filename
# Below for loop finds the .zip file within latest sub-directory
for file_attr in sftp.listdir_attr(path=f'{remote_path}/{latest}'):
if file_attr.filename.endswith('.zip'):
sftp.get(
f'{remote_path}/{latest}/{file_attr.filename}',
f'{local_path}\{file_attr.filename}'
)
print(f'Most recent {branch_name} file successfully retrieved.')
session.close
print('Remote Session Closed.')
感谢您的帮助,非常感谢。
使用SFTPClient.chdir
:
sftp.chdir(path)
或者干脆使用绝对路径或正确的相对路径到家。
糟糕的是,我花了好几个小时研究这个问题,而我做错的只是将一个元组缩减为一个对象……后面没有逗号。这意味着当我遍历我认为是一个元组的东西时,我的代码正在遍历一个字符串——导致我的目标路径不完整。谢谢大家的回复。