How to delete files with a Python script from a FTP server which are older than 7 days?(如何使用 Python 脚本从 FTP 服务器中删除超过 7 天的文件?)
问题描述
我想编写一个 Python 脚本,允许我在文件达到一定年龄后从 FTP 服务器删除文件.我准备了下面的 scipt 但它抛出了错误消息: WindowsError: [Error 3] The system cannot find the path specified: '/test123/*.*'
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!
推荐答案
好的.假设您的 FTP 服务器支持 MLSD
命令,请使用以下代码制作一个模块(这是我用来将远程 FTP 站点与本地目录同步的脚本中的代码):
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):
单目录案例
如果您想处理目录中的文件,您可以:
single directory case
If you want to work on the files of a directory, you can:
这应该做你想做的.
现在,如果这应该递归工作,您必须在单目录案例"的代码中进行以下两项更改:
Now, if this should work recursively, you'll have to do the following two changes in the code for "single directory case":
和
可能的警告
我使用过的服务器在 STOR
和 DELE
命令中的相对路径没有任何问题,因此 site.delete
使用相对路径也可以.如果您的 FTP 服务器需要无路径文件名,您应该首先将 .cwd
到 path
提供的 .delete
普通 ftpfile.name
然后 .cwd
回到基本文件夹.
Possible caveat
The servers I've worked with didn't have any issues with relative paths in the STOR
and DELE
commands, so site.delete
with a relative path worked too. If your FTP server requires pathless filenames, you should first .cwd
to the path
provided, .delete
the plain ftpfile.name
and then .cwd
back to the base folder.
这篇关于如何使用 Python 脚本从 FTP 服务器中删除超过 7 天的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!