简体   繁体   中英

Moving files using shutil.move is too slow

I'm trying to move files from one folder to another in the same HD using the shutil.move . However the process is taking so much time to complete. Commonly in these operations there are too many files (almost 2,000) and together these files have more than 1TB in total.

I looked over the shutil.move documentation and there is explained that the shutil.move uses a copy function behind it (I guess this is why the process is so slow):

shutil.move(src, dst, copy_function=copy2)

Recursively move a file or directory (src) to another location (dst) and return the destination.

If the destination is an existing directory, then src is moved inside that directory. If the destination already exists but is not a directory, it may be overwritten depending on os.rename() semantics.

If the destination is on the current filesystem, then os.rename() is used. Otherwise, src is copied to dst using copy_function and then removed. In case of symlinks, a new symlink pointing to the target of src will be created in or as dst and src will be removed.

If copy_function is given, it must be a callable that takes two arguments src and dst, and will be used to copy src to dst if os.rename() cannot be used. If the source is a directory, copytree() is called, passing it the copy_function(). The default copy_function is copy2(). Using copy() as the copy_function allows the move to succeed when it is not possible to also copy the metadata, at the expense of not copying any of the metadata.

I already tried to change between the copy functions presented in the shutil.move docs, but this didn't improved the time moving the files.

When I try to move the files by myself using ctrl + X , the files are moved instantanely. Anybody knows if there is an alternative method to the shutil.move that I can use which has the same performance of an ctrl + X command?

Thanks in advance.

EDIT

I put all the files that I want to move inside a folder, and then perfom the shutil.move , so my code is just:

src_folder = 'C:/Users/sim/Documents/files_to_move'
dst_folder = 'C:/Users/sim/Documents/_out/'

shutil.move(src_folder, dst_folder)

Also I'm working on Windows 10.

I am not sure why I was having this issue when using shutil.move .

I am working with the same filesystem (In the same hard drive as well) and when I was performing shutil.move(src, dst) the process takes hours to finish, and when I do it manually (using the ctrl-X shortcut) the same files were moved instantly.

I manage to solve the problem using os.rename instead, and now it works perfectly (the files are being moved fast as using ctrl-X shortcut). I will leave this answer here if someone else face the same problem, maybe this could help in some way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM