如何使用Python脚本从FTP服务器上删除超过7天的文件?
我想编写一个 Python 脚本,它允许我在文件达到一定年龄后从 FTP 服务器删除文件。我准备了下面的脚本,但它抛出错误消息:WindowsError:[Error 3]系统找不到指定的路径:'/test123/*.*'
有人知道如何解决此问题吗问题?先感谢您!
import os, time
from ftplib import FTP
ftp = FTP('127.0.0.1')
print "Automated FTP Maintainance"
print 'Logging in.'
ftp.login('admin', 'admin')
# This is the directory that we want to go to
path = 'test123'
print 'Changing to:' + path
ftp.cwd(path)
files = ftp.retrlines('LIST')
print 'List of Files:' + files
#--everything works fine until here!...
#--The Logic which shall delete the files after the are 7 days old--
now = time.time()
for f in os.listdir(path):
if os.stat(f).st_mtime < now - 7 * 86400:
if os.path.isfile(f):
os.remove(os.path.join(path, f))
except:
exit ("Cannot delete files")
print 'Closing FTP connection'
ftp.close()
I would like to write a Python script which allows me to delete files from a FTP Server after they have reached a certain age. I prepared the scipt below but it throws the error message: WindowsError: [Error 3] The system cannot find the path specified: '/test123/*.*'
Do someone have an idea how to resolve this issue? Thank you in advance!
import os, time
from ftplib import FTP
ftp = FTP('127.0.0.1')
print "Automated FTP Maintainance"
print 'Logging in.'
ftp.login('admin', 'admin')
# This is the directory that we want to go to
path = 'test123'
print 'Changing to:' + path
ftp.cwd(path)
files = ftp.retrlines('LIST')
print 'List of Files:' + files
#--everything works fine until here!...
#--The Logic which shall delete the files after the are 7 days old--
now = time.time()
for f in os.listdir(path):
if os.stat(f).st_mtime < now - 7 * 86400:
if os.path.isfile(f):
os.remove(os.path.join(path, f))
except:
exit ("Cannot delete files")
print 'Closing FTP connection'
ftp.close()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
好的。假设您的 FTP 服务器支持 MLSD 命令,请使用以下代码创建一个模块(这是我用来同步远程 FTP 站点与本地目录的脚本中的代码):
module code
single directory case
If如果您想处理目录中的文件,您可以:
这应该可以满足您的要求。
目录及其后代
现在,如果这应该递归地工作,您必须在“单目录情况”的代码中进行以下两项更改:
和
可能的警告
我使用过的服务器没有任何问题
STOR
和DELE
命令中的相对路径,因此具有相对路径的site.delete
也可以工作。如果您的 FTP 服务器需要无路径文件名,您应该首先.cwd
到提供的path
,.delete
普通的ftpfile.name< /code> 然后
.cwd
返回到基本文件夹。OK. Assuming your FTP server supports the
MLSD
command, make a module with the following code (this is code from a script I use to sync a remote FTP site with a local directory):module code
single directory case
If you want to work on the files of a directory, you can:
This should do what you want.
a directory and its descendants
Now, if this should work recursively, you'll have to do the following two changes in the code for “single directory case”:
and
Possible caveat
The servers I've worked with didn't have any issues with relative paths in the
STOR
andDELE
commands, sosite.delete
with a relative path worked too. If your FTP server requires pathless filenames, you should first.cwd
to thepath
provided,.delete
the plainftpfile.name
and then.cwd
back to the base folder.我必须这样做,花了一段时间,我想我可以在这里节省别人的时间。我们使用安装了 ftputil 模块的 python:
I had to do this and it took a while, thought I could save someones time here. We are using python with ftputil module installed:
好吧,与其进一步分析您发布的代码,这里有一个示例,它可能会让您走上正确的轨道。
运行它,您将得到类似这样的输出,这应该是您想要实现的目标的开始。要完成它,您需要将第一个结果解析为日期时间,将其与“now”进行比较,并使用 ftp.delete() 删除远程文件(如果它太旧)。
OK, well rather than analyze the code you have posted any further, here's an example instead that might put you on the right track.
Run it and you'll get output something like this, which should be a start towards what you're trying to achieve. To finish it out you'd need to parse the first result into a datetime, compare it with "now" and use ftp.delete() to get rid of the remote file if it's too old.
好吧,看来您看到的错误与您尝试从本地计算机(而不是 FTP 站点)删除“test123”目录有关。 FTP 文档有一个名为 delete 的方法,这就是您想用它来删除该文件。至于测试某些文件是否已存在 7 天,您实际上可能必须暂时从 FTP 中拉取这些文件,然后在使用 FTP.delete 之前检查修改时间。
Well, it looks like the error you are seeing has to do with the fact that you are trying to remove the 'test123' directory from your local machine, not the FTP site. The FTP docs have a method called delete, and that's what you'd want to use to remove the file. As far as testing whether or not something is 7 days old or not, you might actually have to pull those files down from the FTP temporarily then check the modify times before using FTP.delete.
您在什么操作系统上运行?文件路径
/test123/*.*
是 Unix 风格,但消息显示WindowsError
。您是否正在获取 Unix 风格的 ftp LIST 命令的输出,并尝试在 Windows 脚本中逐字使用它?What OS are you running on? The file path
/test123/*.*
is Unix-style yet the message saysWindowsError
. Are you taking the output of an ftp LIST command, which is in Unix-style, and trying to use it verbatim in a Windows script?