Take the 2-minute tour ×

I want to connect through FTP to an address and then delete all contents. Currently I am using this code:

from ftplib import FTP
import shutil
import os

ftp = FTP('xxx.xxx.xxx.xxx')
ftp.login("admin", "admin")

for ftpfile in ftp.nlst():
    if os.path.isdir(ftpfile)== True:

My problem is I always get this error when he is trying to delete the first file:

WindowsError: [Error 2] The system cannot find the file specified: somefile.sys

Anyone has an idea why ?

share|improve this question
It's a little hard to tell, but it looks like you are using a command to remove a file from the local system, instead of removing the remote file. Can you edit your original question and put in a little more detail? If you don't get an answer, I'll post some source code from our water AMR project next week. I'm not at work now. Why are you using the other Python modules? It's that kind of detail that would help us help you. tnx –  octopusgrabbus Apr 6 '12 at 13:07
Well I am connecting through ftp(first 2 lines) and then I iterate over the files I have on that server ( I can print them) and then I want to delete them one by one (file & folders). First module is for ftp connection, second for deleting directories and third for deleting files. –  vBx Apr 6 '12 at 13:16
add comment

2 Answers

 for something in ftp.nlst():
     except Exception:

Any other ways?

share|improve this answer
add comment

The above statement returns a list of file names.


The above statement requires a file path.

share|improve this answer
Yes, and I am iterating the list and trying to delete the file, I don't think I need a path here –  vBx Apr 6 '12 at 12:38
But doesn't os.remove() remove locally and not remotely corresponding to what ftp.nlst() returned? What am I missing? –  octopusgrabbus Apr 6 '12 at 13:43
docs.python.org/library/os.html says that the argument for os.remove() is path and and not mere file name. –  pravin Apr 6 '12 at 13:43
@octopusgrabbus you are correct. i think it might be possible to check file/directory using ftp.retrlines('MSDL') and then form ftp paths accordingly. any other ways? –  pravin Apr 6 '12 at 13:47
It's in the ftp documentation for Python's ftp, and I don't have my samples. –  octopusgrabbus Apr 6 '12 at 13:49
show 1 more comment

Your Answer


