moving files to new folder in FTP using Python
Question:
I am a bit lost. I’m trying to move a bunch of files to a new folder in FTP using python. I have tried a lot of function but what seems to work best is the ftp.rename function. In fact, it works to move only one file at a time to a new folder but it doesn’t work to do it for a lot of files (like in my screenshot) using a for loop.
Do you know another technique to move efficiently files in a new folder?
Please help
This is the code to move a single file :
ftp = ftplib.FTP(host, username, password)
ftp.encoding = "utf-8"
FtpImage = ftp.mkd("image")
ftp.rename("img1.png", "/image/img1.png")
ftp.quit()
This is the code to move a bunch of files at the same time :
ftp = ftplib.FTP(host, username, password)
ftp.encoding = "utf-8"
#creating a list with all my files
dirList = ftp.nlst()
#creating a folder
ftpFolder = ftp.mkd("folder1")
#moving my file using their name and adding the folder1 to their name
for file in dirList:
ftp.rename(file, "/folder1/" + file)
# shutil.move(file, "/folder1/" + file )
ftp.quit()
Error that I get when I run the second programm:
DeprecationWarning: The Tix Tk extension is unmaintained, and the tkinter.tix wrapper module is deprecated in favor of tkinter.ttk
from tkinter.tix import IMAGETEXT
Traceback (most recent call last):
File "\wsl$Ubuntuhomeq******projet_pythonFTP-sortingtest.py", line 26, in <module>
ftp.rename(file, "/folder1/")
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libftplib.py", line 604, in rename
return self.voidcmd('RNTO ' + toname)
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libftplib.py", line 286, in voidcmd
return self.voidresp()
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libftplib.py", line 259, in voidresp
resp = self.getresp()
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libftplib.py", line 254, in getresp
raise error_perm(resp)
ftplib.error_perm: 550 Rename /folder1/: Device or resource busy
Answers:
I was able to find a way to sort my different files :
I had first to sort my dirList (list with all the files) with new sub list (like allDivers) and then I used the following code
for file in allDivers:
destination_folder = "/divers/"
destination = destination_folder + file
ftp.rename(file, destination )
I am a bit lost. I’m trying to move a bunch of files to a new folder in FTP using python. I have tried a lot of function but what seems to work best is the ftp.rename function. In fact, it works to move only one file at a time to a new folder but it doesn’t work to do it for a lot of files (like in my screenshot) using a for loop.
Do you know another technique to move efficiently files in a new folder?
Please help
This is the code to move a single file :
ftp = ftplib.FTP(host, username, password)
ftp.encoding = "utf-8"
FtpImage = ftp.mkd("image")
ftp.rename("img1.png", "/image/img1.png")
ftp.quit()
This is the code to move a bunch of files at the same time :
ftp = ftplib.FTP(host, username, password)
ftp.encoding = "utf-8"
#creating a list with all my files
dirList = ftp.nlst()
#creating a folder
ftpFolder = ftp.mkd("folder1")
#moving my file using their name and adding the folder1 to their name
for file in dirList:
ftp.rename(file, "/folder1/" + file)
# shutil.move(file, "/folder1/" + file )
ftp.quit()
Error that I get when I run the second programm:
DeprecationWarning: The Tix Tk extension is unmaintained, and the tkinter.tix wrapper module is deprecated in favor of tkinter.ttk
from tkinter.tix import IMAGETEXT
Traceback (most recent call last):
File "\wsl$Ubuntuhomeq******projet_pythonFTP-sortingtest.py", line 26, in <module>
ftp.rename(file, "/folder1/")
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libftplib.py", line 604, in rename
return self.voidcmd('RNTO ' + toname)
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libftplib.py", line 286, in voidcmd
return self.voidresp()
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libftplib.py", line 259, in voidresp
resp = self.getresp()
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libftplib.py", line 254, in getresp
raise error_perm(resp)
ftplib.error_perm: 550 Rename /folder1/: Device or resource busy
I was able to find a way to sort my different files :
I had first to sort my dirList (list with all the files) with new sub list (like allDivers) and then I used the following code
for file in allDivers:
destination_folder = "/divers/"
destination = destination_folder + file
ftp.rename(file, destination )