Moving Files with Python
April 9, 2018
YouTube is one of my favorite resources on the Internet. While there are plenty of good blogs and articles related to data analysis and programming, there’s nothing like a good how-to video. A few weeks ago, YouTube suggested Corey Schafer’s channel to me. Corey has a bunch of great python and linux/mac videos and I’ve recently found his video on the os module to be super helpful.
A few days ago at work, I received about 15 zipped files, each containing two folders with files inside both folders that I needed to use. It was going to be quite a pain to move all of those files into one folder to work on them. Luckily, I had just learned about os.walk()
and had watched sentdex, another YouTuber, work through an error with shutil
so I knew it was possible to write a short script to move the files.
To do this, we’ll be using os.walk()
and shutil.move()
. os.walk()
takes in a directory and “walks” down through all of the directories within the given directory and returns a 3-tuple of dirpath, dirnames and filenames. shutil.move()
takes in 2 arguments, a file or directory and a destination.
Before going through a whole list of files, it’s a good idea to do a quick test on one file. Here’s the test I ran.
import os
import shutil
myFile = '/users/nickbautista/desktop/picturestobemoved/linkedin-pic copy.png'
destDir = '/users/nickbautista/desktop/picturesmovedhere'
shutil.move(myFile, destDir)
print("{} was moved to {}".format(myFile, destDir))
Here, myFile
is the file I want to move and destDir
is the directory where I want the file placed. I’ve added a quick print statement to give some visibility and validation to know that the script has run. Of course, you can always check in the finder/window explorer/terminal to see that the file was moved as well.
Now that we’ve run a short test, we can kick things up a notch using a loop. I’m going to move some pictures as an example. Here’s a quick look at what I have.
Note that almost all of the files in are PNG but there is one JPG. We’re going to add some validation to make sure that file is not moved. Before calling shutil.move()
, I think it’s good practice to again start slow and print the files to ensure that we’re getting the files we want to move.
import os
import shutil
destDir = '/users/nickbautista/desktop/picsinone'
for root, dirs, files in os.walk("/users/nickbautista/desktop/pics"):
for f in files:
if '.png' in f:
fullFile = os.path.join(root, f)
print(fullFile)
We can see that the JPG is not there because of the if
statement. Diving into the code a bit more, we’re looping through the file
that os.walk()
returns and joining the file
with the root
to get exact location of the files we’re after. We’re then using the if
statement to capture only the PNGs and then assigning them to a variable to print out. The variable will be important for us as we get ready to actually move the files.
Since the print
is showing the files we want to move, we can move forward with using shutil.move()
.
import os
import shutil
destDir = '/users/nickbautista/desktop/picsinone'
for root, dirs, files in os.walk("/users/nickbautista/desktop/pics"):
for f in files:
if '.png' in f:
fullFile = os.path.join(root, f)
shutil.move(fullFile, destDir)
print("Check {} for the pics.".format(destDir))
Boom! We’ve successfully moved all of the pictures to one folder. While this is a smaller example, this could save tons of time on a larger list of files buried deeper into folders.
Tips and Tricks
Here are a few tips, should you decide to give this a go.
- Start small by moving one file first and using
print
to double check that you’re capturing the files you want. - Create a backup copy of the original folder just in case things don’t go as planned.
- Try to double check for any duplicate file names. Any duplicates will cause python to throw an error unless you add additional validation to handle that.
Resources
os.walk()
Documentationshutil.move()
Documentationos
Video Corey Schafer
Code Snippets
import os
import shutil
"""
file = '/users/nickbautista/desktop/picturestobemoved/linkedin-pic copy.png'
destDir = '/users/nickbautista/desktop/picturesmovedhere'
shutil.move(file, destDir)
print("{} was moved to {}".format(file, destDir))
"""
destDir = '/users/nickbautista/desktop/picsinone'
for root, dirs, files in os.walk("/users/nickbautista/desktop/pics"):
for f in files:
if '.png' in f:
fullFile = os.path.join(root, f)
# print(fullFile)
shutil.move(fullFile, destDir)
print("Check {} for the pics.".format(destDir))
Questions, comments, concerns? Please feel free to leave a note below.