1

I'm quite new to Python, and am attempting to create a basic backup program, that'll allow me to search for files of a certain extension (in this case, .doc), throughout the entire home drive of a computer, and then copy to a predetermined directory (as the program will be run from a USB). I've got a handle on some of the basic I/O commands, but am having a fair bit of difficulty with this.
Would anyone with the time be able to help me out with this?

Thanks for your time,
Liam.

Liam Johnson
  • 21
  • 1
  • 3

1 Answers1

2

To explore the filesystem, you can try os.walk. It will recursively follow a directory yielding a list of files and dirs in each dir.

For example, given a directory structure like this:

.
├── baz
│   └── bif
│       ├── bang
│       └── file1.doc
└── foo
    ├── bar
    │   └── file3.doc
    └── file2.doc

This code:

import os

print list(os.walk('.'))  # walk current (.) directory

Would produce something like this:

[('.', ['baz', 'foo'], []),
 ('./baz', ['bif'], []),
 ('./baz/bif', ['bang'], ['file1.doc']),
 ('./baz/bif/bang', [], []),
 ('./foo', ['bar'], ['file2.doc']),
 ('./foo/bar', [], ['file3.doc'])]

You could then loop over the results and compile a list of files to copy.

For copying files, the shutil package has copy which just takes src/dest file paths. For more information, see the docs: http://docs.python.org/library/shutil.html

Edit

More helpful file searching things include:

  • The glob package: As the name suggests, glob style file matching (*.txt, ., etc). I don't believe this supports recursive searching though. In this example, if I do glob('foo/*.doc'), I would get the result of ['foo/file2.doc'].
  • fnmatch from the fnmatch package can do glob style pattern matching against strings. Example fnmatch('foo.txt', '*.txt') Would return True
Adam Wagner
  • 15,469
  • 7
  • 52
  • 66
  • Thanks for the feedback. What I had in mind was possibly using the os.walk method you described above to create a list of files to copy, and then use shutil.copyfile to copy all the files in said list. But this raises two problems -- 1) this is way fairly over my head 2) it doesn't seem like the most elegant solution; if I think harder, there's probably a simpler way to handle this. Again, thanks for your help. – Liam Johnson Oct 29 '11 at 04:18
  • @LiamJohnson: the accepted answer to this question: http://stackoverflow.com/questions/2186525/use-a-glob-to-find-files-recursively-in-python gives a good example of how to use os.walk with fnmatch to search for files like you are trying to do. – Adam Wagner Oct 29 '11 at 04:28