0

I'm learning Python, and am going through a course on Codeacademy, but need a particular script as soon as possible.

Essentially, I'm looking for a tool that I can deploy on both Mac and PC that can do the following:

  • Read a source directory
  • Receive a list of keywords from the user
  • Search for those keywords in the file names
  • If a keyword is found in a file name, create a folder with the keyword as its title and move all files with the keyword into the destination folders.

This tool is to be used with Cinema4D, a common 3D graphics package. By default, it renders all of its frames to one directory, but it is often easier to work with the various outputs when they're placed into their own folders.

Take a look at this .zip file. It contains a few .tif files that can serve as a test bed. Ideally, this script could output separate folders called "RGBA" "Object_01" etc.

With your help, I'd like to make this tool both for my own use as well as the entire Cinema4D community (I will give you credit). I have already tried toying with this script, but cannot get the files to be moved. I can only get the folders created, albeit clumsily.

Community
  • 1
  • 1

1 Answers1

2

I whipped this up, cross posted it, deleted it from that question, and re-posted it here, all for you. :-) This requires the 'path.py' module -- pip install path.py or easy_install path.py, and then it should run. ..should does run, because it uses OS agnostic methods -- but I haven't tried it on Windows. Pip (and its requirement, distribute) can be downloaded and installed from here.

#! /usr/bin/python
# -*- coding: utf8 -*-

import os, sys
from path import path



def main(args):
    folder, keyword = path(args[0]), args[1]

    if not folder.exists() and folder.isdir():
        print str(folder) + " is not a valid folder path."
        exit(1)
    targets = []
    for fpath in folder.files():
        if keyword.lower() in fpath.basename().lower():
            targets.append(fpath)
    if targets:
        new_dir = folder / keyword
        new_dir.makedirs_p()
        if new_dir.exists() and new_dir.isdir():
            for fpath in targets:
                dest = new_dir / fpath.basename()
                print "moving {} to {}".format(str(fpath), dest)  
                fpath.move(new_dir)
    else:
        msg = "No files in {} match the keyword {}."
        print msg.format(repr(str(folder)), repr(keyword))

if __name__ == "__main__":
    args = sys.argv[1:]
    help = ('-h', '--help', '/h', '/help', '/?')
    if len(args) != 2 or args[0] in help or args[1] in help:
        print "Moves files in <path> whose name matches <keyword> into a"
        print "subdirectory of <path> named <keyword>"
        print "Usage:"
        print "{} <path> <keyword>".format(sys.argv[0])
        exit(0)
    try:
        main(args)
    except OSError, err:
        print "Failed: " + err.strerror

..this is pretty quick and dirty and doesn't have much help, doesn't use argparse or anything like that, and it has no special options.

  • it handles files only
  • it does not recurse
  • it has pretty darn basic error reporting.
  • be aware that tags match files in a case-insensitive way.

..on the other hand,

  • It works
  • It's fully cross-platform

..if you grabbed this when I first posted it, you might want to do so again, as I've updated it a bit (better error reporting and handling)

Mr. B
  • 2,536
  • 1
  • 26
  • 26