ディレクトリツリーのどこかにある任意のテキストファイル( .txtサフィックス付き)へのパスを取得したいのですが。ファイルを隠したり、隠しディレクトリに置いたりしないでください。
コードを書こうとしましたが、少し面倒に見えます。無駄な手順を避けるために、どのように改善しますか?
def getSomeTextFile(rootDir):
"""Get the path to arbitrary text file under the rootDir"""
for root, dirs, files in os.walk(rootDir):
for f in files:
path = os.path.join(root, f)
ext = path.split(".")[-1]
if ext.lower() == "txt":
# it shouldn't be hidden or in hidden directory
if not "/." in path:
return path
return "" # there isn't any text file