Skip to main content
 首页 » 编程设计

python之在 paramiko python 中的 sftp 期间更改了文件权限

2024年10月25日10xiaohuochai

我正在将目录及其内容从远程递归复制到本地。 但是我发现文件的755权限在复制后在本地已更改为644。

    client = paramiko.SSHClient() 
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
    client.connect(hostname, username=uname, password='password') 
    sftp = client.open_sftp() 
    def copyremotedir(src,dest,sftp):    
        if not os.path.isdir(dest): 
            os.makedirs(dest) 
 
        for item in sftp.listdir_attr(src): 
 
            item_name = str(item.filename) 
            if stat.S_ISDIR(item.st_mode): 
                copyremotedir(src + "/" + item_name, dest + "/" + item_name, sftp) 
            else: 
                sftp.get(src + "/" + item_name, dest + "/" + item_name) 

请您参考如下方法:

scp/sftp 复制文件内容 - 它们不处理文件元数据,例如所有权或权限。

如果您想保留这些,您需要使用 rsync 或创建一个 tar 存档。