python - python删除超过x天的文件夹
问题描述
我想在 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()
解决方案
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 服务器的权限。但这应该是一个好方法。
推荐阅读
- docker - 在 Docker 中转发 redis 仪表板的端口
- javascript - 如何在jqgrid中从mysql中填充选择下拉列表
- c# - WebClient 忽略凭据
- javascript - 在 Flask 中使用 Jquery 添加输入字段
- android - 您可以从自定义类(非活动/片段)访问 ViewModel
- amazon-web-services - 可能的 AllowedOAuthScopes 值?
- angular - 打字稿传递函数作为参数
- react-native - 反应列表中的本地联系人真的很慢
- python - 让 Keras 的 CTC 损失适用于大小差异很大的 Input
- python - 如何正确模拟和测试异步方法调用