Question

    name = raw_input()
    ftp = FTP("")
    ftp.login('','')  #These work fine
    ftp.storbinary("STOR", "%s.txt" % (name)) # I think the issue is here
    ftp.quit()

The program always crashes as it reaches this part, I googled and was unable to find an answer, I have even tried just typing the name of the file, but with the same result.

What am I doing wrong?

Was it helpful?

Solution

If you take a look at the docs, the storbinary method takes the form of ('STOR filename', <file_object>). The issue above is that you don't have a complete STOR command as your first (command) argument. Since you need to pass an open file handler as the file argument, you could try something like:

ftp.storbinary("STOR %s.txt" % (name), open("%s.txt" % name, 'rb'))

Which would create an open file handler based on the name from raw_input (as you're accepting input, you'd want to be wary of malicious input as well). Assuming you handle that, a context manager could be used to open the file (and ensure it closes):

my_file = "%s.txt" % name
with open(my_file, "rb") as f:
    ftp.storbinary("STOR %s" % (my_file), f)

OTHER TIPS

It looks like you need more the just "STOR" as your first argument.

FTP.storbinary(command, file[, blocksize, callback, rest])

Store a file in binary transfer mode. command should be an appropriate STOR command: "STOR filename". file is an open file object which is read until EOF using its read() method in blocks of size blocksize to provide the data to be stored. The blocksize argument defaults to 8192. callback is an optional single parameter callable that is called on each block of data after it is sent. rest means the same thing as in the transfercmd() method.

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