Jump to content

How to delete everything in a FTP folder using Python?

Go to solution Solved by C2dan88,

You first need to get a directory listing of the folder you want to delete use FTP.mlsd() method.

Then loop through the list  of files, pass the filename to  FTP.delete() method if it is a file or if it is a directory call FTP.rmd() method

 

Take a look at this stackoverflow topic for solutions

https://stackoverflow.com/questions/10042838/delete-all-files-and-folders-after-connecting-to-ftp

So I use colab to copy files from my FTP server to my drive using this code (i've 

import os
os.chdir("/content/drive/Shareddrives/HAHSCollage/")
!wget -c -m --ftp-user="username" --ftp-password="password" "ftp://cloudstorage.com/folder/subfolder/Finished/*"

But I have to manually delete all files and folders from the "Finished" folder everytime after I'm done copying.

So, I just need a code that deletes anything in the "Finished" folder, so I'll run it with the same above command.

 

Thanks.

Link to comment
Share on other sites

Link to post
Share on other sites

17 minutes ago, Mr.Stork said:

So I use colab to copy files from my FTP server to my drive using this code (i've 


import os
os.chdir("/content/drive/Shareddrives/HAHSCollage/")
!wget -c -m --ftp-user="username" --ftp-password="password" "ftp://cloudstorage.com/folder/subfolder/Finished/*"

But I have to manually delete all files and folders from the "Finished" folder everytime after I'm done copying.

So, I just need a code that deletes anything in the "Finished" folder, so I'll run it with the same above command.

 

Thanks.

You can use FTPLib and its delete method.

FX6300 @ 4.2GHz | Gigabyte GA-78LMT-USB3 R2 | Hyper 212x | 3x 8GB + 1x 4GB @ 1600MHz | Gigabyte 2060 Super | Corsair CX650M | LG 43UK6520PSA
ASUS X550LN | i5 4210u | 12GB
Lenovo N23 Yoga

Link to comment
Share on other sites

Link to post
Share on other sites

34 minutes ago, igormp said:

You can use FTPLib and its delete method.

I don't want to delete a specific file as you linked, I want to delete anything and all files or folders in the "Finished" folder recursively. I don't want to have to know all the filenames, what I want to do is equavalent to "Ctrl+A" then pressing "Delete".

image.thumb.png.a6259ad71c97f364680412553aa81170.png

Link to comment
Share on other sites

Link to post
Share on other sites

You first need to get a directory listing of the folder you want to delete use FTP.mlsd() method.

Then loop through the list  of files, pass the filename to  FTP.delete() method if it is a file or if it is a directory call FTP.rmd() method

 

Take a look at this stackoverflow topic for solutions

https://stackoverflow.com/questions/10042838/delete-all-files-and-folders-after-connecting-to-ftp

Link to comment
Share on other sites

Link to post
Share on other sites

14 hours ago, C2dan88 said:

You first need to get a directory listing of the folder you want to delete use FTP.mlsd() method.

Then loop through the list of files, pass the filename to FTP.delete() method if it is a file or if it is a directory call FTP.rmd() method

 

Take a look at this stackoverflow topic for solutions

https://stackoverflow.com/questions/10042838/delete-all-files-and-folders-after-connecting-to-ftp

Thanks I found the 2nd answer to be useful, although it deletes the entire folder. So, I just used "ftp.mkd" command to make it again.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×