Skip to content

Python script that would move all of your files to new directories based on their creation and modification dates!

Notifications You must be signed in to change notification settings

triposat/Move_files_according_to_date

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 

Repository files navigation


Prettier Size
sds

Build a Python script that would move all of your files to new directories based on their creation and modification dates. Basically, it will look for directories and, if any are found, it will extract all the files from that folder, delete that folder, and then arrange them by creation date.

Approach:

  • To change the directory and move to the directory where you wish to place all your files based on the modification date, use the os.chdir function.
  • To list all the folders and files, use the os.listdir function.
  • To get the current working directory, use the os.getcwd method.
  • Run a loop to go over all the files within and outside the directories.
  • For storing all the file instances, use the glob.glob function. It will take the file name or file path and search for all the files present inside it.
  • We may simply move files from one location to another by using the shutil.move method. Pass the file name which is to be moved and the path where to be moved.
  • After removing the files from the folder, use the shutil.rmtree methods to remove the folder. Pass the file name to be removed in shutil.rmtree function.
  • Set a loop once more to go through all the files.
  • Use time.gmtime to retrieve all the data about a file’s creation and modifications in the structural form.
  • Then, one by one, extract the Year, Month, and Day.
  • Run an If condition to see if that folder has already been created; if not, create it using the file’s creation date as the name.
  • Finally, using the shutil.move function, move all the files one by one to the newly formed folder.
  • Read complete article here

Output:

Before:

1

After:

2

About

Python script that would move all of your files to new directories based on their creation and modification dates!

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages