Question

Can someone help me.

Why it is not working

import ftplib
import os

def readList(request):
         machine=[]
         login=[]
         password=[]
         for line in open("netrc"): #read netrc file
            old=line.strip()
            line=line.strip().split()
            if old.startswith("machine"): machine.append(line[-1])
            if old.startswith("login"): login.append(line[-1])
            if old.startswith("password"): password.append(line[-1])
            connectFtp(machine,login,password)

def connectFtp(machine,login,password):
  for i in range(len(machine)):
          try:
             ftp = ftplib.FTP(machine[i])
             print 'conected to ' + machine[i]
             ftp.login(login[i],password[i])
             print 'login - ' + login[i] + ' pasword -' + password[i]
           except Exception,e:
             print e
           else:
       ftp.cwd("PublicFolder")
    print 'PublicFolder'

def upload(filename, file):
       readList()
          ext = os.path.splitext(file)[1]
            if ext in (".txt", ".htm", ".html"):
            ftp.storlines("STOR " + filename, open(file))
            else:
             ftp.storbinary("STOR " + filename, open(file, "rb"), 1024)
             print 'success... yra'

upload('test4.txt', r'c:\example2\media\uploads\test4.txt')`

When it was together it was working. But when i separate it in to functions something happened, I cant understand what.

Was it helpful?

Solution

(Apart from the horrid indentation problems, which are presumably due to botched copy and paste otherwise you'd get syntax errors up the wazoo...!)...:

Scoping problem, first: connectFtp makes a local variable ftp so that variables goes away as soon as the function's done. Then upload tries using the variable, but of course it isn't there any more.

Add a return ftp at the end of connectFtp, a yield connectFtp instead of a plain call to the loop in readList, and use a for ftp in readList(): loop in upload.

OTHER TIPS

Something like this?

import os


def readList(request):
    machine = []
    login = []
    password = []
    for line in open("netrc"):  # read netrc file
        old = line.strip()
        line = line.strip().split()
        if old.startswith("machine"): machine.append(line[-1])
        if old.startswith("login"): login.append(line[-1])
        if old.startswith("password"): password.append(line[-1])
        yield connectFtp


def connectFtp(machine, login, password):
    for i in range(len(machine)):


try:
    ftp = ftplib.FTP(machine[i])
    print 'conected to ' + machine[i]
    ftp.login(login[i], password[i])
    print 'login - ' + login[i] + ' pasword -' + password[i]
except Exception, e:
    print e
else:
    ftp.cwd("PublicFolder")
    print 'PublicFolder'
    return (ftp)


def upload(filename, file):
    for ftp in readList():
        ext = os.path.splitext(file)[1]
    if ext in (".txt", ".htm", ".html"):
        ftp.storlines("STOR " + filename, open(file))
    else:
    ftp.storbinary("STOR " + filename, open(file, "rb"), 1024)
    print 'success... yra'

upload('test4.txt', r'c:\example2\media\uploads\test4.txt')

Error at line 19 something with try: unindent does not math any outer indentation level

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top