使用python从多个目录复制特定文件 [英] copy specific files from multiple directories using python

查看:93
本文介绍了使用python从多个目录复制特定文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对python还是很陌生,并且坚持使用我想实现的一段代码。希望我能在这里找到一些帮助。



我有一个特定的文件列表,我想将其复制到一个目录中。我已经在互联网上阅读了很多建议,但我没有读到足够具体的东西。



所以,这是这种情况:我有多个不同的图片文件 FROM文件夹中的文件夹。我只想复制我想要的文件到目标文件夹 TO中。



目前,这就是我所拥有的(来自Stackoverflow上的另一个主题):

  src = /卷/ MacintoshHD / TEST_SHUTIL / FROM 
dst = /卷/ MacintoshHD / TEST_SHUTIL / TO

file_names = [ 120099.TIFF, 901664.TIFF, 902257.TIFF]

用于文件名中的文件名:
full_file_name = os.path.join(src,file_name)
if(os.path.isfile(full_file_name)):
shutil.copy(full_file_name,dst)

我想知道是否可以找到像isfile这样的子文件夹中的文件,但无需测试每个文件夹。



我看过copytree,但是对我来说看起来不是很有用...



起初,我想创建字典wih文件及其url,但比预期的要复杂(出于许多原因),因此最简单的方法是能够在子文件夹中找到这些文件。



许多坦克



巴特。

解决方案

谢谢大家的回答。自从我发布问题以来已经有一段时间了,此后我再也没有时间测试您的答案。因此,请为此辩解。



我已经仔细阅读了它们。我想我理解他们,即使他们没有像这样工作。



我终于设法得到了我想要的东西,但它要尽可能聪明。 / p>

我通过创建文件夹列表来获取文件副本。



这是代码:

  import os,shutil,fnmatch 

src = [ /卷/ MacintoshHD / TEST_SHUTIL / FROM / 1, /卷/ MacintoshHD / TEST_SHUTIL / FROM / 2, /卷/ MacintoshHD / TEST_SHUTIL / FROM / 3]
dst = /卷/ MacintoshHD / TEST_SHUTIL / TO

listePlanDev = [ 00120099.TIFF, 06901664.TIFF, 06902257.TIFF]

用于listePlanDev中的文件名:
用于src中的目录名:
full_file_name = os .path.join(dir_names,file_name)
if(os.path.isfile(full_file_name)):
shutil.copy(full_file_name,dst)

所以这是我刚开始的那棵树:

  \卷
\MacintoshHD
\TEST_SHUTIL
\FROM
\1
-00100001.TIFF
-00120099.TIFF
\2
-06900001.TIFF
-06901664 .TIFF
\3
-06902257.TIFF
\TO

最后我想要

  \卷
\MacintoshHD
\ \TEST_SHUTIL
\FROM
\1
-00100001.TIFF
-00120099.TIFF
\2
-06900001.TIFF
-06901664.TIFF
\3
-06902257.TIFF
\TO
-120099.TIFF
-901664.TIFF
-902257。 TIFF

现在我需要用os.walk在FROM中创建目录列表。因为当我使用程序时应该有成千上万个。



这涉及很多阶段,我敢肯定有一种更好的方法可以减少行数代码...



无论如何,我想给您的答案一个反馈,谢谢大家。



Bart。


I'm quite new to python, and I am stucked with a piece of code I'd like to achieve. I hope I'll be to find some help here.

I have a specific list of files I'd like to copy in one directory. I've read lot of advice on the Internet but I haven't read something specific enough for me.

So, here's the situation : I have multiple pictures files in different folders in "FROM" folder. I'd like to copy only the ones I'd like into a destination folder "TO"

For the moment, here is what I have (from another topic on Stackoverflow) :

src = "/Volumes/MacintoshHD/TEST_SHUTIL/FROM"
dst = "/Volumes/MacintoshHD/TEST_SHUTIL/TO"

file_names = ["120099.TIFF", "901664.TIFF", "902257.TIFF"]

for file_name in file_names:
    full_file_name = os.path.join(src, file_name)
    if (os.path.isfile(full_file_name)):
        shutil.copy(full_file_name, dst)

I wanted to know if there was a way to find the files inside subfolders like a isfile but without testing every folders.

I looked at copytree, but it doesn't look very useful for me...

At first I wanted to create a dictionnary wih the file and its url, but it's more complicated than expected (for many reasons), so the easiest way would be to be able to find those files in subfolders.

Many Tanks

Bart.

解决方案

Thank you every one for your answers. It's been a while since I posted my question, and I hadn't any time to test your answers since. So please excuse me for this.

I have read them carefully. I think I understood them, even if they didn't work like this.

I finally manage to get what I want but it's note as smart as it could be.

I got the copy of files by creating a list of folders.

Here's the code :

import os, shutil, fnmatch

src = ["/Volumes/MacintoshHD/TEST_SHUTIL/FROM/1", "/Volumes/MacintoshHD/TEST_SHUTIL/FROM/2", "/Volumes/MacintoshHD/TEST_SHUTIL/FROM/3"]
dst = "/Volumes/MacintoshHD/TEST_SHUTIL/TO"

listePlanDev = ["00120099.TIFF", "06901664.TIFF", "06902257.TIFF"]

for file_name in listePlanDev :
    for dir_names in src:
        full_file_name = os.path.join(dir_names, file_name)
        if (os.path.isfile(full_file_name)):
            shutil.copy(full_file_name, dst)

So here is the tree I have at he beginning :

\Volumes
    \MacintoshHD
        \TEST_SHUTIL
            \FROM
                \1
                - 00100001.TIFF
                - 00120099.TIFF
                \2
                - 06900001.TIFF
                - 06901664.TIFF
                \3
                - 06902257.TIFF
            \TO

And at the end I'd like

\Volumes
    \MacintoshHD
        \TEST_SHUTIL
            \FROM
                \1
                - 00100001.TIFF
                - 00120099.TIFF
                \2
                - 06900001.TIFF
                - 06901664.TIFF
                \3
                - 06902257.TIFF
            \TO
            - 120099.TIFF
            - 901664.TIFF
            - 902257.TIFF

Now I need to create the list of directories in FROM with os.walk I guess. Beacuase there should be thousands of them when I'll use program.

This makes a lot of stages and I'm sure there's a better way to achieve it in less lines of code...

Anyway, I wanted to give a "feedback" to your answers, and thank you all.

Bart.

这篇关于使用python从多个目录复制特定文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆