【问题标题】:Copy files of certain extension from subdirectories and place them in new subdirectories of same structure从子目录复制某些扩展名的文件并将它们放在相同结构的新子目录中
【发布时间】:2017-11-02 20:37:43
【问题描述】:

我正在浏览计算机列表并在某个目录和子目录中搜索文件扩展名为 .exe.config 的任何内容

我想获取那些以 .exe.config 结尾的文件并将它们放在相同结构的备份目录中。所以这将递归地完成。

例如,“main”是主目录。 “main”中有几个子目录,分别称为“1”、“2”、“3”等

main
\
 \-----------
 \   \    \
 1   2    3

在每个编号的目录中,都会有一个扩展名为 .exe.config 的文件

我要获取主目录名、子目录名和 *.exe.config 文件。然后将它们放在具有相同结构的备份“主”目录中。

在这些编号的目录中还有其他文件,但我想忽略它们。

我无法递归复制所有内容。这是我一直在测试的代码。计算机列表放在 serverlist.txt 中

import os
import shutil
import fileinput

def copy_names(servername):

    source = r'//' + servername + '/c$/Program Files (x86)/Main/'
    dest = r'//' + servername + '/c$/' + servername + '/Main/'

    for root, dirs, files in os.walk(source):
        for file in files:
            if file.endswith('.exe.config'):
                try:
                    os.makedirs(dest, exist_ok=True)
                    shutil.copyfile(os.path.join(source, file), os.path.join(dest, file))
                    print ("\n" + servername + " " + file + " : Copy Complete")
                except:
                    print (" Directory you are copying to does not exist.")

def start():
    os.system('cls' if os.name == 'nt' else 'clear')
    array = []
    with open("serverlist.txt", "r") as f:
        for servername in f:
            copy_names(servername.strip())


# start program
start()

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    尝试这些更改?

    import os
    import shutil
    import fileinput
    
    def copy_names(servername):
    
        source = r'//' + servername + '/c$/Program Files (x86)/Main/'
        dest = r'//' + servername + '/c$/' + servername + '/Main/'
    
        for root, dirs, files in os.walk(source):
            for file in files:
                if file.endswith('.exe.config'):
                    file_src = os.path.join(root, file)
                    file_dest = file_src.replace('Program Files (x86)', servername)
                    os.makedirs(os.path.dirname(file_dest), exist_ok=True)
                    try:
                        shutil.copyfile(file_src, file_dest)
                        print ("\n" + servername + " " + file + " : Copy Complete")
                    except:
                        print (" Directory you are copying to does not exist.")
    
    def start():
        os.system('cls' if os.name == 'nt' else 'clear')
        array = []
        with open("serverlist.txt", "r") as f:
            for servername in f:
                copy_names(servername.strip())
    
    
    # start program
    start()
    

    os.path.join(source, file) 没有给你源文件的正确位置;你应该改用os.path.join(root, file) 之类的东西。

    获得file_dest 可能比file_dest = file_src.replace('Program Files (x86)', servername) 更可靠,但我想不出它是ATM。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-30
      • 1970-01-01
      • 1970-01-01
      • 2020-04-11
      • 2016-09-04
      相关资源
      最近更新 更多