使用Python将所有文件从带日期的SFTP文件夹下载到本地文件夹

时间:2018-09-07 06:37:39

标签: python sftp

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

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。

1 个答案:

答案 0 :(得分:0)

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)