How do you move files from one directory to another using Python?
If you want to move specific files from one directory to another with Python, use the glob
library to fetch the correct files, then use the os
library’s rename
method to change the current file to the new directory.
Here is an example of how this might look using a Python script.
|
|
With the current code I have imported the 2 essential libraries needed to help co-ordinate the change, these being the os
and glob
libraries.
Once the libraries are brought in to the script the first variable captures the current directory where this script is located. By storing this information I can use it to help direct from the current script location to where the current files are located and where they need to go.
Therefore, the next command directs where to find the files I would like to move. You will notice within this line that the last parameter entered into the os.path.join()
function is an asterisked file name *.py
.
This will be a path that glob
will be able to recognise and capture within the path.
Currently the structure of my project looks something like this:
As you can see from the above folder structure the file where this information will be stored is outside the directories of the old_loc
and new_loc
.
Irrespective of where the script is located the important thing to note is making sure you correctly identify the path of the files you wish to move.
The next part of the script captures all the files from the old_loc
directory:
|
|
The above line of code captures all the files according to the path passed in from the variable old_files
.
This means the variable files_to_move
will contain a list of string paths to each file satisying the constraints of old_files
.
The next part is to loop through each of the string paths and move the file to it’s new location.
This is how the next part of the code will be constructed using a for-loop to iterate through each of the path strings:
|
|
This final part of the code loops through each of the path strings in the files_to_move
variable and then using the os.path.basename()
method extracts just the file name from the path so that the same file name can be used in the new directory.
This obviously assumes the contents of the directory where the files are going to be relocated to will not cause any naming clashes. If this is a possibility you might want to check for the existence of new_file
before moving otherwise you will lose files already existing in the new location.
You can read more about iterating through file names here.
Once the current file name is captured it is used to create a new path with os.path.join()
. With the new path string obtained the final step is to use the os.rename()
function to move a file from one location to another.
Here is the entire snippet of code in its entirety here:
|
|
After the above script is run in the terminal using:
$ python3 my_script.py
The result of the directory structure is as follows:
The beauty of this process is that there’s no need to make copies or to delete unwanted files. The process is simple and effectively moves all the files acccording to the path and notation used with the glob.glob()
function.
Summary
Moving a file in Python can easily be achieved by using the os
and glob
libraries. The os
module helps to provide an easy way to create a path, whereas the glob
module helps to find the files you want to move (as maybe you only want to move files of a certain type).
Creating a Python script to tie all these bits together is trivial.