2017-01-25 21 views
2

こんにちは私は別のファイルに名前を変更する必要があります。私はこれまでのところこれを持っていますが、私はそれを持っていて、それぞれを置き換える代わりに多くのアイテムと対応する置換えを持たせて、コードを実行してからもう一度再入力することができます。Pythonスクリプトは、フォルダとサブフォルダ内のすべてのファイルの名前を再帰的に変更します

UPDATEは*また、私は、ファイルの一部を変更する名前の変更を必要としない全部それだけでそれを変更するだろうに「「Cat5e50m1mBED_50m2mBE2U-Aqeoiu31" 「Cat5e_1mBend1bottom50m2mBend2top-Aqeoiu31」があったので、もし

import os, glob 

#searches for roots, directory and files 
for root,dirs, files in os.walk(r"H:\My Documents\CrossTalk\\"): 
    for f in files: 
     if f == "Cat5e_1mBend1bottom50m2mBend2top":#string you want to rename 
      try: 
      os.rename('Cat5e_1mBend1bottom50m2mBend2top', 'Cat5e50m1mBED_50m2mBE2U')) 
      except FileNotFoundError, e: 
      print(str(e)) 
+0

作成するファイル名にはどのような置き換えがありますか? –

答えて

3

あなたはこのwathを望みますか?

import os, glob 
#searches for roots, directory and files 
#Path 
p=r"C:\\Users\\joao.limberger\\Documents\\Nova Pasta" 
# rename arquivo1.txt to arquivo33.txt and arquivo2.txt to arquivo44.txt 
renames={"arquivo1.txt":"arquivo33.txt","arquivo2.txt":"arquivo44.txt"} 
for root,dirs,files in os.walk(p): 
    for f in files: 
     if f in renames.keys():#string you want to rename 
     try: 
      os.rename(os.path.join(root , f), os.path.join(root , renames[f])) 
      print("Renaming ",f,"to",renames[f]) 
     except FileNotFoundError as e: 
      print(str(e)) 

これがあなたの希望であるかどうか確認してください。

import os, glob 
#searches for roots, directory and files 
#Python 2.7 
#Path 
p=r"C:\\Users\\joao.limberger\\Documents\\Nova Pasta" 
# if the substring in the key exist in filename, replace the substring 
# from the value of the key 
# if the key is "o1" and the value is "oPrinc1" and the filename is 
# arquivo1.txt ... The filename whil be renamed to "arquivoPrinc1.txt" 
renames={"o1":"oPrinc1","oldSubs":"newSubs"} 
for root,dirs,files in os.walk(p): 
    for f in files: 
     for r in renames: 
      if r in f: 
       newFile = f.replace(r,renames[r],1) 
       try: 
        os.rename(os.path.join(root , f), os.path.join(root , newFile)) 
        print "Renaming ",f,"to",newFile 
       except FileNotFoundError , e: 
        print str(e) 
+0

'f in renames.keys()' => 'if fより多くのpythonic、より速い。 –

+0

それは動作しませんでした、それはすべてのファイルの名前を変更しました。私はファイルの名前の一部だけを変更したい。私があなたを混乱させている場合は申し訳ありません、上記のアップデートを見てください – VisualExstasy

+0

ありがとう@ Jean-FrançoisFabre私はPythonプログラミングの新機能です!!!! –

2
あなたが必要があると思い

最初の事はあなたのコードの小さな変化、その後、交換のためのdictionary次のとおりです。name_mapで

import os, glob 

name_map = { 
    "Cat5e_1mBend1bottom50m2mBend2top": 'Cat5e50m1mBED_50m2mBE2U' 
} 

#searches for roots, directory and files 
for root,dirs,files in os.walk(r"H:\My Documents\CrossTalk"): 
    for f in files: 
     if f in name_map: 
      try: 
      os.rename(os.path.join(root, f), os.path.join(root, name_map[f])) 
      except FileNotFoundError, e: 
      #except FileNotFoundError as e: # python 3 
      print(str(e)) 

key(「:」の左にある文字列)の名前です。フィルeをファイルシステムに、value( ":"の右にある文字列)を使用する名前にします。

+1

これは動作しません。あなたは 'root'ディレクトリに参加する必要がありますか、' rename'が失敗します。 –

+0

@ Jean-FrançoisFabreねえ、最後のスクリプトで助けてくれました、何か提案がありますか? – VisualExstasy

+0

よろしくお願いします。@ Jean-FrançoisFabre –

関連する問題