您可以使用ftplib在Python中提供完整的FTP支持.但是,获取目录列表的首选方法是:
# File: ftplib-example-1.py
import ftplib
ftp = ftplib.FTP("www.python.org")
ftp.login("anonymous", "ftplib-example-1")
data = []
ftp.dir(data.append)
ftp.quit()
for line in data:
print "-", line
Run Code Online (Sandbox Code Playgroud)
产量:
$ python ftplib-example-1.py
- total 34
- drwxrwxr-x 11 root 4127 512 Sep 14 14:18 .
- drwxrwxr-x 11 root 4127 512 Sep 14 14:18 ..
- drwxrwxr-x 2 root 4127 512 Sep 13 15:18 RCS
- lrwxrwxrwx 1 root bin 11 Jun 29 14:34 README -> welcome.msg
- drwxr-xr-x 3 root wheel 512 May 19 …Run Code Online (Sandbox Code Playgroud) 我想写一个Python脚本,它允许我在达到一定年龄后从FTP服务器中删除文件.我准备了下面的scipt但它抛出了错误消息:WindowsError: [Error 3] The system cannot find the path specified: '/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 …Run Code Online (Sandbox Code Playgroud) 我想列出ftp目录上的所有目录,然后输入每个目录。问题是我的代码还会列出文件并尝试输入它们。
这是我现在使用的代码:
from ftplib import FTP
ftp = FTP('ftp.overtherainbow.com')
ftp.login()
for name in ftp.nlst():
print "listing: " + name
ftp.cwd(name)
ftp.retrlines('LIST')
ftp.cwd('../')
Run Code Online (Sandbox Code Playgroud)