在Python中搜索带有扩展名的文件并复制到目录?

发布于 2024-12-12 09:25:09 字数 190 浏览 0 评论 0原文

我对 Python 很陌生,正在尝试创建一个基本的备份程序,它允许我在计算机的整个主驱动器中搜索特定扩展名的文件(在本例中为 .doc),并且然后复制到预定目录(因为程序将从 USB 运行)。我已经掌握了一些基本的 I/O 命令,但在这方面遇到了相当大的困难。
有时间的人可以帮我解决这个问题吗?

感谢您的宝贵时间,
利亚姆.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橙幽之幻 2024-12-19 09:25:09

要探索文件系统,您可以尝试 os.walk。它将递归地跟踪一个目录,在每个目录中生成文件和目录的列表。

例如,给定如下的目录结构:

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

此代码:

import os

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

会生成如下内容:

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

然后您可以循环结果并编译要复制的文件列表。

对于复制文件,shutil 包具有 copy,它仅采用 src/dest 文件路径。有关更多信息,请参阅文档: http://docs.python.org/library/shutil.html

编辑

更有用的文件搜索内容包括:

  • glob 包:顾名思义,glob 样式文件匹配(*.txt、. 等)。我不相信这支持递归搜索。在此示例中,如果我执行 glob('foo/*.doc'),我将得到 ['foo/file2.doc'] 的结果。
  • fnmatch 包中的 fnmatch 可以对字符串进行 glob 样式模式匹配。示例 fnmatch('foo.txt', '*.txt') 将返回 True

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文