2017-03-22 12 views
0

私はコーディングとこのフォーラムに慣れていないので、私と一緒に裸にしてください!Pythonを使用してフォルダ構造を検索し、XMLファイルを変更する

私は、フォルダ構造を反復し、 'bravo'という名前のフォルダを見つけ、それらの中に含まれているxmlファイルを変更するpythonスクリプトを作成しようとしています。

xmlファイルでは、 'file'というタグの 'location'属性を変更したいと考えています。次のようにしてください:

ファイルパスのドライブ文字を「f」から「f」に変更するだけです。それが読まれるように:

<file location="f:\one\two"/> 

をしかし...

これらのXMLファイルの名前は一意であるので、私は正確なxmlファイル名を検索することはできません。代わりにxmlファイルタイプで検索しています。

他のxmlファイルも、 'file'タグの参照を除いて無視されます。

私が修正したいxmlファイルは、すべて「bravo」という名前のフォルダに保存されています。

また、正常に更新されたすべてのxmlファイルとファイルパス(できれば失敗したファイルパス)を一覧表示するログファイルを作成したいと考えています。

このサイトの同様の質問に対する回答を使用して、私は以下のスクリプトをまとめました。

現在の状態では、スクリプトは見つかったすべてのxmlファイルを変更しようとします。私は、 'bravo'という名前のフォルダだけを検索するコードを正常に追加することができませんでした。

スクリプトは 'bravo'フォルダではなくxmlファイルを変更すると、これらのファイルに 'file'タグが含まれていないためにエラーが発生します。

私のスクリプトを修正する(または新しいスクリプトを作成する)人を助けてください。ここではフォルダ構造の例がある

...

My folder structure

そして、これまでのところ、私のスクリプトは...

from xml.dom import minidom 
    import os 

    # enter the directory where to start search for xml files... 

    for root, dirs, files in os.walk("c:/temp"): 
     for file in files: 
      #search for xml files... 
      if file.endswith(".xml"): 
       xml_file = file 

       xmldoc = minidom.parse(os.path.join(root, xml_file)) 

       # in the xml file look for tag called "file"... 
       file_location = xmldoc.getElementsByTagName("file") 
       # i don't understand the next line of code, but it's needed 
       file_location = file_location[0] 

       # 'location_string' is a variable for the 'location' path of the file tag in the xml document 
       location_string = (file_location.attributes["location"].value) 

       # the new drive letter is added to the location_string to create 'new_location' 
       new_location = "f" + location_string[1:] 

       # replace the 'location' value of the file tag with the new location... 
       file_location.attributes["location"].value = new_location 

       # write the change to the original file 
       with open((os.path.join(root, xml_file)),'w') as f: 
       f.write(xmldoc.toxml()) 
       print "%s has been updated!" % (os.path.join(root, xml_file)) 
       # add updated file name to log... 
       log_file = open("filepath_update_log.txt", "a") 
       log_file.write("%s\n" % (os.path.join(root, xml_file))) 
       log_file.close 

すべてのヘルプは大歓迎されます!

答えて

0
  1. 2番目のループの前にディレクトリ名が適合するかどうかをテストします。パスの最後のディレクトリを最初に取得する必要があります。以下のように:How to get only the last part of a path in Python?

    if os.path.basename(os.path.normpath(root)) == "bravo": 
    
  2. あなたはロギング用https://docs.python.org/3/library/logging.htmlモジュールを使用することができます。

  3. 1文字だけを置き換えたい場合は、xmlを解析する代わりに直接置き換えることができます。で示唆したように:おかげでヒープがhttps://stackoverflow.com/a/17548459/7062162

    def inplace_change(filename, old_string, new_string): 
        # Safely read the input filename using 'with' 
        with open(filename) as f: 
         s = f.read() 
         if old_string not in s: 
          print('"{old_string}" not found in {filename}.'.format(**locals())) 
          return 
    
        # Safely write the changed content, if found in the file 
        with open(filename, 'w') as f: 
         print('Changing "{old_string}" to "{new_string}" in {filename}'.format(**locals())) 
         s = s.replace(old_string, new_string) 
         f.write(s) 
    
+0

...あなたのポイント1は、御馳走を働いたと私は、ロギングモジュール上に読んであげます。 – Ant78

関連する問題