-2

3 つのことを行う Python スクリプトを作成したいと思います。

  • 1) ユーザー入力を取得して、ファイル ディレクトリに移動します
  • 2) ファイルの内容を確認します (スクリプトを続行するには、特定のファイルのセットがフォルダーにある必要があります)。
  • 3) 検索と置換を行う
  • 現在のコード:

    import os, time
    from os.path import walk
    
    mydictionary = {"</i>":"</em>"}
    
    for (path, dirs, files) in os.walk(raw_input('Copy and Paste Course Directory Here: ')):
        for f in files:
            if f.endswith('.html'):
                filepath = os.path.join(path,f)
                s = open(filepath).read()
                for k, v in mydictionary.iteritems(): terms for a dictionary file and replace
                    s = s.replace(k, v)
                f = open(filepath, 'w')
                f.write(s)
                f.close()
    

    今、私はパート 1 と 3 を持っています。パート 2 だけが必要です。

    パート 2 では、ユーザーが指定するディレクトリに html ファイルのみが存在することを確認する必要があります。それ以外の場合、スクリプトはユーザーに正しいフォルダー ディレクトリ (html ファイルを含む) を入力するように求めます。

    ありがとう

    4

    1 に答える 1