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