2017-11-02 7 views
1

私はコンピュータのリストを通過すると.exe.configファイルのコピーと同じ構造

Iのファイル拡張子とは何のためにある特定のディレクトリとサブディレクトリを探していますの新しいサブディレクトリに配置します.exe.configで終わるファイルを取り出し、同じ構造のバックアップディレクトリに置いてください。これは再帰的に行われます。

たとえば、「main」はメインディレクトリです。などの番号のディレクトリのそれぞれにおいて

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

を、「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() 

答えて

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_destfile_dest = file_src.replace('Program Files (x86)', servername)よりも得るより堅牢な方法ですが、私はそれをATMと考えることはできません。

関連する問題