Opened 7 years ago

Closed 7 years ago

#60 closed defect (wontfix)

'listdir' strips whitespace from beginning of filenames on FTP site

Reported by: ftputiluser Owned by: schwa
Priority: minor Milestone: 2.7
Component: Library Version: 2.6
Keywords: listdir whitespace Cc:

Description (last modified by schwa)

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.

Change History (4)

comment:1 Changed 7 years ago by ftputiluser

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

comment:2 Changed 7 years ago by schwa

Description: modified (diff)
Status: newassigned

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 of ls -l on Posix system or dir 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's ftplib module, so I have to rely on ftplib doing the right thing. That doesn't always seem to be the case. For example, I noticed that if I use ftplib's mkd 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.

comment:3 in reply to:  2 Changed 7 years ago by schwa

Replying to schwa:

ftputil uses Python's ftplib module, so I have to rely on ftplib doing the right thing. That doesn't always seem to be the case. For example, I noticed that if I use ftplib's mkd 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 the mkd method.

comment:4 Changed 7 years ago by schwa

Resolution: wontfix
Status: assignedclosed

I decided not to "solve" the problem for the reasons given in my first comment. Sorry.

Note: See TracTickets for help on using tickets.