現在のフォルダーとそのすべてのサブフォルダーを参照して、拡張子が.htm | .htmlのすべてのファイルを取得します。オブジェクトが次のようなdirであるかファイルであるかを見つけることが可能であることがわかりました。
import os
dirList = os.listdir("./") # current directory
for dir in dirList:
if os.path.isdir(dir) == True:
# I don't know how to get into this dir and do the same thing here
else:
# I got file and i can regexp if it is .htm|html
最後に、すべてのファイルとそのパスを配列に入れたいと思います。そのようなことは可能ですか?
を使用os.walk()
して、ディレクトリとそのすべてのサブディレクトリを再帰的に反復できます。
for root, dirs, files in os.walk(path):
for name in files:
if name.endswith((".html", ".htm")):
# whatever
これらの名前のリストを作成するには、リスト内包表記を使用できます。
htmlfiles = [os.path.join(root, name)
for root, dirs, files in os.walk(path)
for name in files
if name.endswith((".html", ".htm"))]
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加