【问题标题】:Downloading All Files from SFTP folder to local folder with Date using Python使用 Python 将所有文件从 SFTP 文件夹下载到带有日期的本地文件夹
【发布时间】:2018-09-07 06:37:38
【问题描述】:

我正在使用以下代码下载文件,

def download_dir(remote_dir, local_dir):
     import os
     os.path.exists(local_dir) or os.makedirs(local_dir)
     dir_items = sftp.listdir_attr(remote_dir)
     for item in dir_items:
        # assuming the local system is Windows and the remote system is 

        # os.path.join won't help here, so construct remote_path manually
        remote_path = remote_dir + '/' + item.filename         
        local_path = os.path.join(local_dir, item.filename)
        if S_ISDIR(item.st_mode):
            download_dir(remote_path, local_path)
        else:
            sftp.get(remote_path, local_path)


download_dir("/home","C:\\Users\\ShareM\\Desktop")

如何使用 DateStamp 将其下载到文件夹中?例如,如果我今天下载,它下载到的文件夹应该命名为 07/09/2018。

【问题讨论】:

    标签: python sftp


    【解决方案1】:

    datetime 模块与os 一起使用

    例如:

    import os
    import datetime
    
    
    local_dir = "C:\\Users\\ShareM\\Desktop"
    folderName = os.path.join(local_dir, datetime.datetime.now().strftime("%d-%m-%Y"))    #Create Folder with todays date
    os.path.exists(folderName) or os.makedirs(folderName)
    

    【讨论】:

      猜你喜欢
      • 2021-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-26
      • 1970-01-01
      • 2019-02-20
      相关资源
      最近更新 更多