0

I am trying to upload a file to an ftp server with python using the ftplib.

This is what i have:

def ftp(cmd):

    cmd = cmd.split(' ')
    try: cmd[3]
    except: return 'host user password file (ftpdir)'
    try: session = ftplib.FTP(cmd[0],cmd[1],cmd[2])
    except: return 'wrong credentials/host'
    try: file = open(cmd[3], 'rb')
    except: return 'unable to reach file'
    try: cmd[4]
    except: pass
    else: 
        if cmd[4] !='':
            ftplib.FTP.cwd(ftpdir)
    name = file.split('\\')[-1]
    session.storbinary('STOR ' + name, file)     # send the file
    file.close()                                    # close file and FTP
    session.quit()

I give the function a command in the form of 'host user password file ftpdir' where ftpdir is not required. The error I get is this one:

Traceback (most recent call last):

  ...some lines of referring...

  File "C:\somedir\somefile.py", line 155, in ftp
    file = open(cmd[3],'rb')
TypeError: open() takes exactly 1 argument (2 given)

If i try the command "file = open(cmd[3], 'rb')" with a given 'cmd' as entry in a python shell it works fine.

tshepang
  • 12,111
  • 21
  • 91
  • 136
user1952084
  • 67
  • 2
  • 9
  • 4
    What do your import statements look like? To me, it looks like the definition of the `open` function is being overwritten. – Fredrick Brennan Jan 06 '13 at 01:37
  • Is your code really the way you've included it? It seems like that exception should be eaten by the `try`/`except` block around the `open` call (which is one reason that a bare `except` is a bad idea!). – Blckknght Jan 06 '13 at 01:48
  • Oh god, I see it now. I defined another function with open... Thank you @frb . Sorry about that, it is working fine now. – user1952084 Jan 06 '13 at 13:56

1 Answers1

0

This question is now answered. The problem was that I defined another function open(arg) which took exactely one argument. After changing the name of that function, everything worked fine.

Thank you for your time everyone who read this.

user1952084
  • 67
  • 2
  • 9