如何使用 Paramiko 下载昨天的文件?
How do I download files from yesterday with Paramiko?
我需要下载 mod昨天确定日期的所有 SFTP 文件。这是我最近的尝试。我有点相信它与时间戳有关,但所有试图从两个变量中去除时间戳的尝试都没有解决我的问题。下面的代码下载了 14 个文件中的 11 个,mod 日期为昨天时间戳 12:29:05 AM 和 2:28:46 AM 之间。 3 个丢失的文件的时间戳为 11:35:25 到 11:29:34。我正在使用 SQL 服务器代理来安排 运行,并且服务器 运行 将脚本作为我硬编码到脚本中的同一服务帐户。
from os.path import basename
import os.path
import paramiko
import datetime
from datetime import date, timedelta
#Assigning variables to upload file to SFTP
host = 'SERVER'
port = PORT
transport = paramiko.Transport((host, port))
##setting up transport
username = 'UNAME'
password = 'PW'
##connecting to FTP
transport.connect(hostkey = None, username = username, password = password,pkey = None)
sftp = paramiko.SFTPClient.from_transport(transport)
##create yesterday var and normalize it
yesterday = date.today() - timedelta(days=1)
yDay = yesterday.day
yYear = yesterday.year
yMonth = yesterday.month
##create a list of all files and get name/modified date of the fiels.
for file in sftp.listdir_attr('WORKING DIRECTORY'):
i = file.st_mtime
filedate = date.isoformat(i)
fdDay = filedate.day
fdYear = filedate.year
fdMonth = filedate.month
if fdDay == yDay and fdYear == yYear and fdMonth == yMonth:
remotepath = 'WORKING DIRECTORY' + file.filename
localpath = 'LOCAL PATH' + file.filename
sftp.get(remotepath, localpath)
#Close Connection
sftp.close()
transport.close()
首先计算昨天开始和结束的时间戳:
from datetime import datetime, time, timedelta
today_midnight = datetime.combine(datetime.today(), time.min)
yesterday_midnight = today_midnight - timedelta(days=1)
(基于What was midnight yesterday as an epoch time?)
然后将这些时间戳与文件的时间戳进行比较:
for file in sftp.listdir_attr('/remote/path'):
mtime = datetime.fromtimestamp(file.st_mtime)
if (yesterday_midnight <= mtime) and (mtime < today_midnight):
print(file.filename)
我需要下载 mod昨天确定日期的所有 SFTP 文件。这是我最近的尝试。我有点相信它与时间戳有关,但所有试图从两个变量中去除时间戳的尝试都没有解决我的问题。下面的代码下载了 14 个文件中的 11 个,mod 日期为昨天时间戳 12:29:05 AM 和 2:28:46 AM 之间。 3 个丢失的文件的时间戳为 11:35:25 到 11:29:34。我正在使用 SQL 服务器代理来安排 运行,并且服务器 运行 将脚本作为我硬编码到脚本中的同一服务帐户。
from os.path import basename
import os.path
import paramiko
import datetime
from datetime import date, timedelta
#Assigning variables to upload file to SFTP
host = 'SERVER'
port = PORT
transport = paramiko.Transport((host, port))
##setting up transport
username = 'UNAME'
password = 'PW'
##connecting to FTP
transport.connect(hostkey = None, username = username, password = password,pkey = None)
sftp = paramiko.SFTPClient.from_transport(transport)
##create yesterday var and normalize it
yesterday = date.today() - timedelta(days=1)
yDay = yesterday.day
yYear = yesterday.year
yMonth = yesterday.month
##create a list of all files and get name/modified date of the fiels.
for file in sftp.listdir_attr('WORKING DIRECTORY'):
i = file.st_mtime
filedate = date.isoformat(i)
fdDay = filedate.day
fdYear = filedate.year
fdMonth = filedate.month
if fdDay == yDay and fdYear == yYear and fdMonth == yMonth:
remotepath = 'WORKING DIRECTORY' + file.filename
localpath = 'LOCAL PATH' + file.filename
sftp.get(remotepath, localpath)
#Close Connection
sftp.close()
transport.close()
首先计算昨天开始和结束的时间戳:
from datetime import datetime, time, timedelta
today_midnight = datetime.combine(datetime.today(), time.min)
yesterday_midnight = today_midnight - timedelta(days=1)
(基于What was midnight yesterday as an epoch time?)
然后将这些时间戳与文件的时间戳进行比较:
for file in sftp.listdir_attr('/remote/path'):
mtime = datetime.fromtimestamp(file.st_mtime)
if (yesterday_midnight <= mtime) and (mtime < today_midnight):
print(file.filename)