Search code examples
pythonmacosfilemove

Python starter program : Moving files


Looking to make some use out of Python! I wanted to write a script to move all those carelessly placed .jpg files from my desktop.

Thing, is the script I'm using doesn't seem to be finding anything.

thoughts?

import os, shutil, glob

dst_fldr = "~/path/Desktop/newfolder"; 

for jpg_file in glob.glob("~/path/Desktop"+"\\*.jpg"):
    print jpg_file + "will be moved to " + dst_fldr
    shutil.move(jpg_file, dst_fldr);

Solution

  • ~ is not a character that glob understands (it's a character that bash understands and expands). You'll have to provide a full path.

    dst_fldr = "/path/to/Desktop/newfolder"; 
    

    In addition, you'd want to modify the wildcard search, to something like this:

    glob.glob("/path/to/Desktop/*.jpg"):
    

    If your python script resides in Desktop, you can drop the /path/to/Desktop part of the path in both cases.

    With these changes in place, I believe you're good to go.