2

I've been working on a little python program that simply connects to my nas and cleans up some of my filenames. I'm using the python ftp lib to connect to the nas and do my things.

I'm just wondering here if anyone has a good way of checking whether a file on the server is actually a file or if it is a folder?

This is what I'm using:

try:
     ftp.cwd(line) 
     #If we got here then this "line" is a folder
     # Do my folder stuff
     ftp._ftp.cwd('..') #don't forget to go back after it worked
except ftplib.error_perm:
     #An exception! So this means we are dealing with a file
     #So do some file stuff
except:
     #Sometimes you just can't get in the folder for some reason
     falsepos = falsepos + 1

I could look at the output of the directory listing but this is not the same on different platforms and I am trying to build something what will keep on working even if I for some reason replace my nas.

There is also a little bug in my code. Some times it still regards a folder as a regular file...

Your opinions?

(I'm using Python 2.7)

Mike
  • 1,003
  • 1
  • 11
  • 23

1 Answers1

2

I don't find any differences between platforms. On which platforms does this not work?:

isFile = lambda e: e[0][0] != 'd'
for e in ftp.dir('.'):
  if isFile(e):
    foo(e)
  else:
    bar(e)
Trylks
  • 1,458
  • 2
  • 18
  • 31
  • I read in previous posts about this topic that there are some differences between some ftp systems. And that there is no standard output. For example: http://stackoverflow.com/q/111954/1393744 . So that is why I prefer not to check it in that manner. If I am wrong, please do tell me. – Mike May 21 '12 at 20:44
  • The [RFC for FTP](http://www.faqs.org/rfcs/rfc959.html) states the LIST command isn't good for automatic use (search for "(LIST)"). – Trylks May 22 '12 at 14:28
  • Sorry, unexpected behaviour of "enter". Python is a programming language, its libraries should work in an automatic context, not just as a cool command line. If there are differences between platforms, ftplib should abstract the supported platforms, if it does not that's an enhancement proposal for Python. I'm sorry I don't know which is the case, so far this code worked for me, but I can't guarantee it will always work. – Trylks May 22 '12 at 14:48
  • You are absolutely right. Thank you for a clear and constructive answer! – Mike May 22 '12 at 16:47
  • Obviously if there are any other points of view on this topic, I would love to hear them. – Mike May 22 '12 at 16:48