2

I'm writing a Python program centered around transferring files from a PC to a target device. So far I've used the ftplib package for all the other functions in the program. I've encountered a problem that didn't happen in any of the other devices so far, that the ftp.pwd() returns just an empty string, but when I use the ftp.voidcmd("PWD") - which is the same as ftp.pwd() I get my working directory.

Here is the snippet that relates to the problem, note the I have done the same with a few other devices and it never happened:

# CONNECT TO TARGET
ftp = FTP (host)
ftp.login(username , password)

ftp.cwd("/DirName")
# DOESNT WORK
res = ftp.pwd()
print(res)
# WORKS
res = ftp.voidcmd("PWD")
print(res)
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Ben Pinhas
  • 63
  • 6

1 Answers1

1

The ftplib is strict about the response code returned by the PWD command. Only if the code is 257 as mandated by RFC 959, the ftplib will consider the response valid and will return the path.

Some obscure FTP servers do not use the 257 code, but some other 2xx code. Many FTP clients are tolerant and will handle such response as valid. The ftplib is not. You will have to keep using your workaround with FTP.voidcmd("PWD").

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992