A call to 'listdir' will return a list of filenames in a particular
directory on an FTP server. However, these filenames appear to be
stripped of any whitespace on the beginning of the filename. Thus, the
following code will fail if one of the files on the FTP server has a
name with a leading space, throwing a ftputil.ftp_error.PermanentError
(Not Found):
ftplist = host.listdir(host.curdir)
for ftpfile in ftplist:
host.remove(ftpfile)
'ftpfile' contains the filename without the leading space, leading to the conclusion that listdir is faulty.
Just to attach a couple more details to this, I'm using:
- FTPutil 2.6
- Python 2.7
- ProFTPD 1.3.3 Server (Hddn FTP Server01)
- Client OS: Ubuntu 11.10, Server OS unknown
Thanks for the report.
The names are extracted from the listing data that is retrieved with an FTP
DIR
command. This gives a listing similar to the output ofls -l
on Posix system ordir
on Windows systems.Both the Unix format parser and the Microsoft format parser in ftputil use
str.split()
which splits at sequences of whitespace. Hence there's no distinction between a space which is part of a filename and a space for column alignment in the directory listing.I have one or two ideas on how to handle this to allow names with whitespace. However, this would have to use some heuristics and won't be a robust approach by any means. Additionally, I'm not sure if I could handle filenames with leading/trailing spaces in them correctly even if I managed to extract them correctly from the listings.
ftputil
uses Python'sftplib
module, so I have to rely onftplib
doing the right thing. That doesn't always seem to be the case. For example, I noticed that if I useftplib
'smkd
command with a directory name with a leading space, the leading space is converted to an underscore.I very much tend to close this ticket as "wontfix": The "fix", if there's any, would be brittle, and there's no guarantee either that the names will be processed correctly in following FTP commands. My advice is to avoid leading and trailing whitespace (or if possible any whitespace) in directory or file names if you need to process them with a protocol which relies on parsing that's not strictly defined.
Replying to schwa:
ftputil
uses Python'sftplib
module, so I have to rely onftplib
doing the right thing. That doesn't always seem to be the case. For example, I noticed that if I useftplib
'smkd
command with a directory name with a leading space, the leading space is converted to an underscore.I looked at the
ftplib
source code. It seems the FTP server put in the leading underscore, not themkd
method.
I decided not to "solve" the problem for the reasons given in my first comment. Sorry.