tags:

views:

41

answers:

2
import ftplib
server = '192.168.1.109'
user = 'bob'
password = 'likes_sandwiches'

box = ftplib.FTP(server)
box.login(user, password)

s = box.mkd('\\a\\this4\\')
box.close()

x = raw_input('done, eat sandwiches now')

This returns:

Traceback (most recent call last): File "C:\scripts\ftp_test.py", line 25, in s = box.mkd('\E\this4\') File "C:\Python26\lib\ftplib.py", line 553, in mkd return parse257(resp) File "C:\Python26\lib\ftplib.py", line 651, in parse257 raise error_reply, resp error_reply: 250 Directory created successfully.

It successfully created a directory, but it thinks its an error! WTF?

I plan on creating many directories in a loop, how can I do this without having it break every time it successfully creates a single directory?

+1  A: 

According to RFC 959 (FTP), the only valid response code to MKD is 257. Looks like this is a problem caused by the FTP server not conforming to the standard.

For your interest, this is the relevant ftplib code:

if resp[:3] != '257':
    raise error_reply, resp
Scytale
ok, looks like my FTP server talks with an accent. There's no way for me to change it. How can I form a loop to create directories and ignore all these exceptions it will throw at me then?
chazzycheese
@ruffiko, with the voidcmd method, as I mentioned in my answer.
Alex Martelli
Instead of ignoring exceptions, you should probably go for Alex’ answer and use voidcmd.
Scytale
+1  A: 

ftplib is expecting a result of 257, defined as " created", so it can parse the <pathname> and return it for you; but your server is surprisingly giving a result of 250 and does not return the pathname, so the mkd method of course fails.

As a workaround to this peculiar server behavior, you can use voidcmd to just send the MKD /your/path command -- after all, you know the pathname you want to create, since it's an absolute one.

Alex Martelli