我想通过 sftp 下载一些超过 2 小时的文件。然后我想从网站上删除它们。我可以将以下代码用于 sftp,但在远程机器上处理对象给我带来了问题。下面的代码在“timestamp = os.stat”行失败我相信这是一个操作系统模块问题?
import paramiko, sys, os,time
host = 'ftp address'
port = 22
transport = paramiko.Transport((host, port))
password = "pass" #hard-coded
username = "user" #hard-coded
transport.connect(username = username, password = password)
sftp = paramiko.SFTPClient.from_transport(transport)
print 'SFTP Client initiated'
remotepath = "/remote folder/"
localpath = '/local folder/'
for file in sftp.listdir('.'):
fullpath = os.path.join('.',file)
timestamp = os.stat(fullpath).st_ctime # get timestamp of file
createtime = datetime.datetime.fromtimestamp(timestamp)
now = datetime.datetime.now()
delta = now -createtime
if delta.hours > 2:
sftp.get(file,localpath)
sftp.remove(file)
sftp.close()
transport.close()