为什么我要在ftp服务器上创建一个超过x天的python脚本。
我已经完成了ftp部分,但是要删除我没有解决方案的文件夹,可以有人召集该波纹管,您可以找到我到目前为止的代码。
ftp = FTP('ftp.my.webhosting.be')
ftp.login('user@localhost.be', 'xyz')
path = 'Export/XML/Stocks/_PROCESSED'
print('Changing Directory to : ' + path)
ftp.cwd(path)
# List Contents
ftp.dir()
# Get the currecnt time
now = time.time()
# Delete folders older than 5 days
print('Closing FTP connection')
ftp.close()
答案 0 :(得分:0)
from dateutil import parser
ftp = FTP('ftp.my.webhosting.be')
ftp.login('user@localhost.be', 'xyz')
path = 'Export/XML/Stocks/_PROCESSED'
print('Changing Directory to : ' + path)
ftp.cwd(path)
# List Contents
files = ftp.dir()
# Get the currecnt time
now = time.time()
# Delete folders older than 5 days
for file in files:
timestamp = ftp.voidcmd(f"MDTM {file}")[4:].strip() # Not sure if correct, but as an indication
fileTime = parser.parse(timestamp) # you have to double check these, but this would be my approach
timediff = fileTime - now # Again not sure if this would work, but good approach
# if (timediff > 5 days):
# deletefile()
print('Closing FTP connection')
ftp.close()
这可能是一种方法,我不知道此特定代码是否有效,因为我没有访问FTP服务器的权限。但这应该是一个好方法。
答案 1 :(得分:0)
#! /usr/bin/python
import time
import ftputil
host = ftputil.FTPHost('ftphost.com', 'username', 'password')
mypath = 'your_path'
now = time.time()
host.chdir(path)
names = host.listdir(host.curdir)
for name in names:
if host.path.getmtime(name) < (now - (5 * 86400)):
if host.path.isfile(name):
host.remove(name)
print 'Closing FTP connection'
host.close()
答案 2 :(得分:-1)
找到了调用rm树和一些其他婴儿车所需的解决方案
host.rmtree(name, ignore_errors=False, onerror=None)