3

I'm new to python and I'm trying to read an ftp directory and write the filenames and file sizes to a file (currently a text file)

import sys
import os
import ftplib
import ftputil
import fnmatch
log = open("C:/..../ftp_name.txt","a")
print "logging into FTP" # print 
host = ftputil.FTPHost('address','Uname','Pass') # ftp host info
recursive = host.walk("/WORLDVIEW",topdown=True,onerror=None) # recursive search 
for root,dirs,files in recursive:
    for name in files:
        fullpath = os.path.join(root, name)
        size = FTP.size(fullpath)
        writepath = fullpath + " " +size + "\n"
        log.write(writepath)

I got it to write the filename and path but once I added the size function in it went wrong

The error I received was:

<b>NameError: name 'FTP' is not defined</b>

I have also tried replacing

size = FTP.size(fullpath)

with

size = recursive.size(fullpath)

which returned the error:

<b>AttributeError: 'generator' object has no attribute 'size'</b>

2 Answers 2

1

Generally to get file size you use the os module: https://docs.python.org/2/library/os.path.html#os.path.getsize.

When using ftputil they make that an equivlant call of host.path.getsize

You can view more of the documentation for it here: http://mcdc.missouri.edu/libs/python/ftputil/ftputil.html#retrieving-information-about-directories-files-and-links

   ...
   for root,dirs,files in recursive: 
       for name in files: 
           fullpath = host.path.join(root, name)
           size = host.path.getsize(fullpath)
5
  • Hi, I also tried that but I received this error: <b>WindowsError: [Error 3] The system cannot find the path specified: '/WORLDVIEW/...21075\\LABEL.txt'</b> I'm not sure where the double slash came from
    – MattEnvSys
    Commented May 15, 2014 at 12:31
  • I updated the answer to use ftputil, hopefully that works for you! Commented May 15, 2014 at 12:32
  • Hmm my error just became a lot more complex after updating the code! ** File "C:\Python27\lib\site-packages\ftputil\ftp_stat.py", line 452, in _real_lstat "550 %s: no such file or directory" % path) ftputil.ftp_error.PermanentError: 550 /WORLDVIEW/DEVI/DEVI_FIN_I027161_21075\LAB EL.txt: no such file or directory Debugging info: ftputil 2.2.3, Python 2.7.3 (win32)** that's the end of the error. I suppose I need to play around with the path as it places a \ ahead of label.txt instead of /?
    – MattEnvSys
    Commented May 15, 2014 at 12:44
  • You are correct the error is the last "\" is different than all the "/"s before it. Basically FTP using unix folder structure, and when you call "os.path.join" it automatically joins using the systems default, which in your case is windows. Use host.path.join instead. Commented May 15, 2014 at 12:48
  • Great to hear, please accept the answer if it's working for you as is :) Commented May 15, 2014 at 12:56
1

I know you got an answer but

size = FTP.size(fullpath) FALSE maybe you copied it from https://docs.python.org/2/library/ftplib.html

You see all of the functions have FTP.function() it just a headline

try using

size = ftp.size(fullpath)

am amazed no one pointed that out for almost 3 years

NameError: name 'FTP' is not defined === check the name

I know because I had the same error.

1
  • Yep... you're right, except he called his ftp connection host, so it would be host.size(fullpath) Working for me
    – blissweb
    Commented May 4, 2020 at 11:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.