Move Files to Creation and Modification Date Named Directories using Python


In this problem statement we have to move all the files in the folder with the name of creation and modification date directory with the help of Python. In this task we will use some predefined libraries of Python for smooth processing of relocating the files.

Understanding the Problem

As we have seen, managing the files as per the date or modification and creation time. So here in this article we will simplify this task by creating the Python script to move the files as per the creation and modification dates by giving the folder name as the modification or creation date. So basically we will look for that folder and move the files and if the folder is not present as per the date then we will create the new folder by giving the name as per the date of the modification of the file and move those files by arranging them in order of creation or modification date.

In the above image we can see the files and modified date of these files. So we will mode these files in the modification date folder to arrange them.

Algorithm

  • Step 1 − First import the necessary libraries in the beginning of the Python script. In our program we are importing OS, shutil and datetime libraries.

  • Step 2 − Define the source file path in which the files are present to perform the move operation.

  • Step 3 − Then change the current working directory with the source directory. And then list all the directories and files in the current directory.

  • Step 4 − So we will need a working folder so we will use the getcwd function of the os library.

  • Step 5 − Now we will loop through all the files of the current working directory using the for loop.

  • Step 6 − Inside the loop we will join the current directory path with the file or directory name.

  • Step 7 − Check the condition, if we are on the right path then move all the files to the current directory. And remove the directory when files are moved.

  • Step 8 − And if the right path has not been found then get the modification date of the file and create the directory and name it as the modification date. And move all the files to the destination folder.

Example

import os as system
import shutil
import datetime

source_directory = r"F:/Python Tutorial/Tutorialspoint folder/test"

# Change the current working directory to the source directory
system.chdir(source_directory)

# List all directories and files in the current directory
all_files = system.listdir()

# need the current working folder
output = system.getcwd()

# Loop over each file in the current folder
for dir_or_file in all_files:
   try:
      # Join the current directory path with the file/directory name
      path = system.path.join(output, dir_or_file)

      if system.path.isdir(path):
         # move directory's files to the current directory
         dir_files = system.listdir(path)
         for dir_file in dir_files:
            file_path = system.path.join(path, dir_file)
            shutil.move(file_path, output)

         # When the files are moved so remove the directory
         shutil.rmtree(path)
      else:
         # Get file's modification date
         modify_time = system.path.getmtime(path)
         modify_date = datetime.datetime.fromtimestamp(modify_time)

         # Create the directory as per the modification date
         dir_name = modify_date.strftime("%b-%d-%Y")
         dest = system.path.join(output, dir_name)

         # If directory is not present the Create it
         system.makedirs(dest, exist_ok=True)

         # Move the file to the destination folder
         shutil.move(path, dest)
   except Exception as e:
      print(f"Error occurred: {e}")

print("Successfully moved the files.")

Output

$$\mathrm{Before \: Moving}$$

$$\mathrm{After \: Moving}$$

Conclusion

So we have created a code to move all the files to the creation and modification date named directories with the help of Python. So basically we have used os, shutil and datetime libraries of Python. We have iterated through all the files and directories in the source path and move the field to the corresponding directory.

Updated on: 16-Oct-2023

61 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements