What method should I use to move files?

Asked 2 years ago, Updated 2 years ago, 85 views

source_files = '/PATH/TO/DIR1/*'
destination_folder = '/PATH/TO/DIR2'

When the source and destination are decided like that, I want to move the file

I looked up the os module, but there is no copy function, so I ask you a question here.

mv (mv /PATH/TO/DIR1/* /PATH/TO/DIR2) with Linux commands.

Which module has a function that does the same thing as ?

file python move

2022-09-22 13:50

1 Answers

os.rename() or shutil.move().

os.rename (src, dst, *, src_dir_fd=None, dst_dir_fd=None) rename the file/directory from src to dst.

shutil.move(src, dst, copy_function=copy2) recursively moves the file/directory (src) to another location (dst)

import os
os.rename("/PATH/TO/DIR1/*", "/PATH/TO/DIR2")

import shutil
shutil.move("/PATH/TO/DIR1/*", "/PATH/TO/DIR2")


2022-09-22 13:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.