Julian Drago
Julian Drago

Reputation: 749

Python FTP ftplib move an entire folder to another directory fails with "Cannot link to a file on another device"

I've come across various answers on how to move files from one location to another using Python ftplib (such as How to move and replace files from FTP folder to another folder in same FTP). I'm trying to move an entire folder, including all subfolders and files, to another location.

Say I have a folder, /FromPath/MoveThisFolder, and this folder can have an unknown number of subfolders and files, ie /FromPath/MoveThisFolder/A and /FromPath/MoveThisFolder/B, both of which contain files, etc.

I want to move MoveThisFolder to /ToPath, such that I get /ToPath/MoveThisFolder with all contained subfolders and files moved over and no longer existing in /FromPath/MoveThisFolder.

How might I accomplish this?

From what I can tell, ftp.rename is meant for files, per the Python documentation:

FTP.rename(fromname, toname)

Rename file fromname on the server to toname.

If I try using ftp.rename(), I get an error that I can't resolve:

ftp.rename(ftp_from_loc, ftp_to_loc)
error_perm: 550 rename: Cannot link to a file on another device.

I also tried using mv within ftp.sendcmd() based on this question, but I get an error

ftp.sendcmd('mv ' + ftp_from_loc + ' ' + ftp_to_loc)

error_perm: 500 'MV /sourcePath/* /destinationPath/': command not understood.

(Of course I've replaced the actual paths with dummy names, but I've triple checked the paths and they're correct).

Upvotes: 0

Views: 404

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202168

There is not difference between moving/renaming a file or a folder on most systems. So FTP.rename can be used for directories as well.

What is generally not supported (in general, not in FTP only) is moving a file (or a directory) to another file system (let alone another drive/device). And it seems that this is exactly what you are attempting. Moving to another file system is not really moving, it would have to involve copying all files and deleting originals. While a real moving does not copy any data, it just moves the metadata.

Upvotes: 0

Related Questions