Method Name:
dir
Method Signature:
dir(directory_name[, …])
Parameters:
- The dir() method accepts variable number for parameters.
- When invoked without parameters the function sends the LIST command to the server.
- When invoked with one or more parameters, if the last parameter is a callback, then all other parameters except the callback function are concatenated to the LIST command separated by a space and the command is sent to the server.
- If the last parameter passed is a callback function the function is called for each line of output from the server.
Return Type:
None
Overview:
- The method dir() prints the directory listing as returned by the FTP command LIST.
- Among the number of arguments passed to the function if the last argument is a callback function, this function is called for each line of the output from the server.
Example:
from ftplib import FTP
def dirCallback(line): line = "%s **"%line; print(line);
ftpObject = FTP(); ftpObject.connect(host="anexampleftpserver"); ftpObject.login(); print("Current directory listing:"); ftpObject.dir();
fileName = "50MB.zip"; print("Information on the file:%s"%fileName); ftpObject.dir(fileName);
directoryName = "/upload" print("Information on a specific directory:%s"%directoryName); ftpObject.dir(directoryName);
print("Invoke a non-default callback on everyline for current directory:"); ftpObject.dir(".", dirCallback); |
Output:
Current directory listing: -rw-r--r-- 1 0 0 52428800 Feb 19 2016 EF.txt -rw-r--r-- 1 0 0 524288 Feb 19 2016 CD.txt -rw-r--r-- 1 0 0 5242880 Feb 19 2016 AB.txt drwxr-xr-x 2 105 108 561152 Oct 09 09:09 uploadfolder Information on the file:AB.txt -rw-r--r-- 1 0 0 52428800 Feb 19 2016 AB.txt Information on a specific directory:/upload -rw------- 1 105 108 1000000 Oct 09 09:00 tbd.txt -rw------- 1 105 108 494276 Oct 09 09:09 tbr.txt Invoke a non-default callback on everyline for current directory: -rw-r--r-- 1 0 0 52428800 Feb 19 2016 EF.txt ** -rw-r--r-- 1 0 0 524288 Feb 19 2016 CD.txt ** -rw-r--r-- 1 0 0 5242880 Feb 19 2016 AB.txt ** drwxr-xr-x 2 105 108 561152 Oct 09 09:09 uploadfolder ** |