File Relocation Utility

  • Share this:

Code introduction


This function moves a file from a specified path to a specified directory. If the target directory does not exist, it will be created automatically.


Technology Stack : Python, shutil, os

Code Type : File operation

Code Difficulty : Intermediate


                
                    
import shutil
import random

def move_file_to_dir(file_path, destination_dir):
    if not os.path.exists(destination_dir):
        os.makedirs(destination_dir)
    shutil.move(file_path, os.path.join(destination_dir, os.path.basename(file_path)))                
              
Tags: