2009-07-07 3 views

答えて

12

globモジュールを使用してください。

>>> import glob 
>>> glob.glob('./[0-9].*') 
['./1.gif', './2.txt'] 
>>> glob.glob('*.gif') 
['1.gif', 'card.gif'] 
>>> glob.glob('?.gif') 
['1.gif'] 
+0

チャームのように働いて、この非常に速い答えのおかげで! – JustRegisterMe

2

グロブ答えは簡単ですが、完全を期すために:あなたはまた、os.listdirと正規表現のチェックを使用することができます。

import os 
import re 
dirEntries = os.listdir(path/to/dir) 
for entry in dirEntries: 
    if re.match(".*tmp.*\.log", entry): 
    print entry 
+0

reを使用する必要はありません。 – ghostdog74

+0

良いアイデアは、*以上のものを必要とするより複雑な検索に適しています。 – JustRegisterMe

0

以下のコードを示すことによって、以前の回答に拡大より複雑な検索ケース。

構成ファイルによって大いに制御されたアプリケーションがありました。実際には、それぞれ異なるトレードオフを持つ設定の多くのバージョンがありました。したがって、1つのコンフィギュレーションセットは徹底した作業になりますが、非常に遅くなる一方で非常に遅くなりますが、徹底的ではありません。したがって、GUIには、コンフィギュレーションに応じたオプションを持つコンフィギュレーションコンボボックスがあります。時間の経過と共に構成のセットが増えていると感じたので、アプリケーションのリストとその対応するオプション(およびその順序)をハードコードするのではなく、このすべてを伝えるファイル命名規則に頼っていました情報。

私が使用した命名規則は次のとおりです。ファイルは$ MY_APP_HOME/datディレクトリにあります。ファイル名は、my_config_で始まり、その後にコンボ番号が続き、その後にコンボ項目のテキストが続きます。たとえば、ディレクトリにmy_config_11_fast_but_sloppy.txt、my_config_100_balanced.txt、my_config_3_thorough_but_slow.txtというファイルが含まれていた場合、コンボボックスのオプションは(その順番に)次のようになります。

だから、実行時に、私は、ディレクトリ

  • に応じてコンボボックスに入れ
  • ソート・オプションするすべてのファイル名からオプションのリストを抽出するには、私の設定ファイルを検索し

    1. に必要なインデックス
    2. は以下

    MyConfigurationクラスは、Alを行い、選択されたオプションからファイルパスを取得することができここで

    # populate my_config combobox 
    self.my_config = MyConfiguration() 
    self.gui.my_config.addItems(self.my_config.get_items()) 
    
    # get selected file path 
    index = self.gui.my_config.currentIndex() 
    self.config_file = self.my_config.get_file_path_by_index(index); 
    

    はMyConfigurationクラスです:

    import os, re 
    
    class MyConfiguration: 
        def __init__(self): 
         # determine directory that contains configuration files 
         self.__config_dir = ''; 
         env_name = 'MY_APP_HOME' 
         if env_name in os.environ: 
          self.__config_dir = os.environ[env_name] + '/dat/'; 
         else: 
          raise Exception(env_name + ' environment variable is not set.') 
         # prepare regular expression 
         regex = re.compile("^(?P<file_name>my_config_(?P<index>\d+?)_(?P<desc>.*?)[.]txt?)$",re.MULTILINE) 
         # get the list of all files in the directory 
         file_names = os.listdir(self.__config_dir) 
         # find all files that are our parameters files and parse them into a list of tuples: (file name, index, item_text) 
         self.__items = regex.findall("\n".join(file_names)) 
         # sort by index as an integer 
         self.__items.sort(key=lambda x: int(x[1])) 
    
        def get_items(self): 
         items = [] 
         for item in self.__items: 
          items.append(self.__format_item_text(item[2])) 
         return items 
    
        def get_file_path_by_index(self, index): 
         return self.__config_dir + self.__items[index][0] 
    
        def __format_item_text(self, text): 
         return text.replace("_", " ").title(); 
    
    それが:-)目的を説明するために連れて行ってくれましたし、それは次のように使用できることリットルわずか数行のコード(での作業が大幅に少ないです
  • 関連する問題