4

I am an absolute beginner to programming so I apologize if this is really basic. I've looked at other questions that appear to be related, but haven't found a solution to this particular problem--at least not that I can understand.

I need to generate a list of files in a directory; create a separate directory for each of those files with the directory name being based on each file's name; and put each file in its corresponding directory.

Bach
  • 6,145
  • 7
  • 36
  • 61
user3245142
  • 81
  • 1
  • 2
  • What do you mean by "based on each file's name?" Please be more specific. – merlin2011 Apr 23 '14 at 21:59
  • Merlin2011, perhaps an example would help. – user3245142 Apr 24 '14 at 02:13
  • For example, if the filename is `foo`, should the directory name be `PREFIX_foo`? Or maybe `foo` with the first letter capitalized? – merlin2011 Apr 24 '14 at 02:14
  • Ex. In a directory, I have 21 files: seven with a basename of Austin and different extensions; seven named Syracuse with the same seven extensions; and seven named Davenport, again with seven different extensions. From my script, I want to be able to read this or any folder, determine the basenames (Austin, Syracuse, and Davenport in this ex.), create a new folder per basename (so, three new folders named Austin, Syracuse, and Davenport), and move all of the Austin files into the Austin folder, Syracuse files into the Syracuse folder, and Davenport files into Davenport file. Make sense? – user3245142 Apr 24 '14 at 02:37
  • (Sorry--I timed out, so I've just resubmitted my example, now.) – user3245142 Apr 24 '14 at 02:37
  • I just realized that my example might still not be clear. I may have multiple sets of files that need to be separated by basenames into individual directories. Any clearer? – user3245142 Apr 24 '14 at 02:40

2 Answers2

8

You should have a look at the glob, os and shutil libraries.

I've written an example for you. This will remove the file extension of each file in a given folder, create a new subdirectory, and move the file into the corresponding folder, i.e.:

C:\Test\
 -> test1.txt
 -> test2.txt

will become

C:\Test\
 -> test1\
    -> test1.txt
 -> test2\
    -> test2.txt

Code:

import glob, os, shutil

folder = 'C:/Test/'

for file_path in glob.glob(os.path.join(folder, '*.*')):
    new_dir = file_path.rsplit('.', 1)[0]
    os.mkdir(os.path.join(folder, new_dir))
    shutil.move(file_path, os.path.join(new_dir, os.path.basename(file_path)))

This will throw an error if the folder already exist. To avoid that, handle the exception:

import glob, os, shutil

folder = 'C:/Test/'

for file_path in glob.glob(os.path.join(folder, '*.*')):
    new_dir = file_path.rsplit('.', 1)[0]
    try:
        os.mkdir(os.path.join(folder, new_dir))
    except WindowsError:
        # Handle the case where the target dir already exist.
        pass
    shutil.move(file_path, os.path.join(new_dir, os.path.basename(file_path)))

PS: This will not work for files without extensions. Consider using a more robust code for cases like that.

Steinar Lima
  • 7,644
  • 2
  • 39
  • 40
3

Here is some advice on listing files using Python.

To create a directory, use os.mkdir (docs). To move a file, use os.rename (docs) or shutil.move (docs).

Community
  • 1
  • 1
user3553031
  • 5,990
  • 1
  • 20
  • 40