-2

I am trying to download files using python script from my ftp server...However i am getting the files which are of size 0 kb...i can't understand exactly where i am wrong...I am actually searching the files by a particular string in filename and then downloading all the files having that string on my ftp in a given directory.

Here is my code:

# Libraries
import re
import os
import ftplib
import ntpath

ftp = ftplib.FTP("192.168.1.786:22")
ftp.login("Marshmellow", "YourPasswordHere")
##ftp.dir("feed_1")

files = []

## F = open('Files.txt','a')

try:
    files = ftp.nlst("feed_1")
    for fname in files:
        res = re.findall("2018-07-25", fname)
        if res:
           # Open the file for writing in binary mode
            print 'Opening local file ' + ntpath.basename(fname)
            file = open(ntpath.basename(fname), 'wb')

            # Download the file a chunk at a time
            # Each chunk is sent to handleDownload
            # We append the chunk to the file and then print a '.' for progress
            # RETR is an FTP command

            print 'Getting ' + ntpath.basename(fname)
            try:
                ftp.retrbinary('RETR ' + ntpath.basename(fname), file.write)
            except:
                pass
            # Clean up time
            print 'Closing file ' + ntpath.basename(fname)
            file.close() 
            print (fname)
##          F.write(fname + '\n')
        if not res:
            continue
except ftplib.error_perm , resp:
    if str(resp) == "550 No files found":
        print "No files in this directory"
        pass
    else:
        raise

## F.close()

Help Me Out if anyone knows what's wrong in this.

Marshmellow
  • 111
  • 1
  • 11
  • `expect: pass` is not a good when debugging a problem. – Klaus D. Aug 01 '18 at 14:02
  • I am quite new to python...Didn't knew that...thanks @KlausD. But the problem still remains. – Marshmellow Aug 01 '18 at 14:13
  • Don't you need to specify port number as well to the ftp server?nI have used it as `server = ftplib.FTP() server.connect(, )` – RoadRunner Aug 01 '18 at 14:15
  • Tried doing that also @RoadRunner...It still downloads file with size 0kb. – Marshmellow Aug 01 '18 at 14:35
  • And what happens if you try to download the file with a command line FTP? – Serge Ballesta Aug 01 '18 at 15:54
  • You really posting login-information to your ftp? I tried with filezilla, it said Server and User do even exist... Even with a changed password, you can be target of cyber attacks. My Browser said it couldn't reach the Server, so maybe you have to check your IP? Or are the logins only "slightly wrong"? – Grimmauld Aug 02 '18 at 20:35
  • These are not the true login details of my ftp...it's Some Dummy Stuff @S.Bender....You need to try this code by putting your own ftp address,Username and password. – Marshmellow Aug 03 '18 at 10:56
  • Command line download is working fine for me... having issues with this one only...@SergeBallesta – Marshmellow Aug 03 '18 at 10:57
  • i guess i am not getting the correct file path while the file with the name goes for download. is it the issue here guys? – Marshmellow Aug 04 '18 at 05:54
  • 1
    It looks like you have solved your problem as you have posted a [follow up question](https://stackoverflow.com/q/51684008/850848). So please either post your solution or delete your question. – Martin Prikryl Aug 06 '18 at 09:15
  • Oops! Sorry @MartinPrikryl ... i forgot to put my answer here. The issue there was the filename i was getting is as follows : feed_1/---filename---- thats why the files were of 0 kb size. the was just searching for the string and collecting the files from there only...not actually downloading them. What i did to solve the problem is as follows: – Marshmellow Aug 06 '18 at 11:21
  • try:ftp.cwd("feed_1") files = ftp.nlst() for fname in files: res = re.findall("2018-07-25", fname) if res: # Open the file for writing in binary mode print 'Opening local file ' + ntpath.basename(fname) file = open(ntpath.basename(fname), 'wb') i've just set the current working directory using ftp.cwd("feed_1") which i did the wrong way earlier like: files = ftp.nlst("feed_1") ....And now the problem is gone ! Thanks Everyone for your Help! :) – Marshmellow Aug 06 '18 at 11:24
  • Please *do not post code in the comments* - it is literally unreadable! Post the solution as an *answer* instead – desertnaut Aug 06 '18 at 12:09

1 Answers1

0
   try:
    ftp.cwd("feed_1") 
    files = ftp.nlst() for fname in files: 
    res = re.findall("2018-07-25", fname) if res: 
    # Open the file for writing in binary mode 
    print 'Opening local file ' + ntpath.basename(fname) 
    file = open(ntpath.basename(fname), 'wb')

i've just set the current working directory using ftp.cwd("feed_1") which i did the wrong way earlier like: files = ftp.nlst("feed_1")

Marshmellow
  • 111
  • 1
  • 11