Computer >> Computer tutorials >  >> Programming >> Python

How to move a file from one folder to another using Python?


The shutil module provides functions for moving files, as well as entire folders. For moving multiple files at once, you'll have to have a list of all files you want to copy and loop over them to copy them.

Calling shutil.move(source, destination) will move the file at the path source to the folder at the path destination. (Both source and destination are strings.) If destination is a filename, it will be used as the new name of the moved file. This function returns a string of the path of the moved file. 

example

import shutil, os
files = ['file1.txt', 'file2.txt', 'file3.txt']
for f in files:
    shutil.move(f, 'dest_folder')