python - 如何从python知道sftp中的当前工作目录

标签 python operating-system sftp paramiko

我已经通过一些细节连接到 sftp,以使用 paramiko 从 python 中获取一些文件 代码如下

import paramiko
import os
import ftplib

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy( paramiko.AutoAddPolicy() )
ssh.connect('sftp.example.com', username='user', password='pass')
ftp = ssh.open_sftp()
files = ftp.listdir()
print files,"----< Total files"
print ftp.cwd(),"---< Current working diectory"

for feedFile in files:
    if feedFile == 'LifeBridge':
        ftp.chdir(feedFile)

结果

['Emerson', 'Lehighvalley', 'LifeBridge', 'stmaryct'] ----< Total files

File "test_sftp.py", line 11, in <module>
    print ftp.cwd()   ---< Current working diectory
AttributeError: 'SFTPClient' object has no attribute 'cwd'

我现在做的是

  1. 试图找到sftp的当前工作目录

  2. 有一个文件列表,上面是我打印文件的结果,我正在尝试检查它们是文件夹还是文件, 如果它们是文件夹,我想进入 LifeBridge 文件夹

最后谁能告诉我以下内容

  1. 如何查看sftp当前工作目录
  2. 如何检查上面列表的结果是文件还是文件夹

最佳答案

您正在寻找 .getcwd() method相反:

Return the "current working directory" for this SFTP session, as emulated by paramiko. If no directory has been set with chdir, this method will return None.

因此在 session 开始时,如果您需要当前工作目录,请先尝试 .chdir('.')(更改为当前目录)。

.listdir() 方法只返回目录中项目的名称。如果您需要有关这些文件的更多信息,则需要使用 .listdir_attr() method相反,它返回 SFTPAttributes instances 的列表.

SFTPAttributes 实例通过 .st_mode 属性告诉您文件的模式。使用 stat.S_ISDIR(mode) function测试文件是否为目录。

关于python - 如何从python知道sftp中的当前工作目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12404168/

相关文章:

java - 使用 JSch 执行 SFTP 时无法连接到安全通道

python - 计算某列在恢复为 0 之前为正值的时间量

python - 对多索引数据帧的行求和,但在缺失数据中显示 0

Java:是否可以覆盖操作系统设置的最小窗口宽度?

multithreading - 'critical section algorithm' 中的进度和有限等待是什么?

c# - 在 SSH.NET 中进行多因素身份验证( key 和密码)时获取 'Permission denied (password).'

python - python线程和进程之间的管道有不同的行为吗?

python - 如何修复属性错误: module 'tensorflow' has no attribute 'reset_default_graph'

Android - 开机动画,加载开机动画前的静态图片

java - jsch session.connect() 问题