Skip to content

Instantly share code, notes, and snippets.

@eyasuyuki
Created March 25, 2026 06:14
Show Gist options
  • Select an option

  • Save eyasuyuki/b4f06b70c4a0985479b96d83c3cca549 to your computer and use it in GitHub Desktop.

Select an option

Save eyasuyuki/b4f06b70c4a0985479b96d83c3cca549 to your computer and use it in GitHub Desktop.
ファイルパスとディレクトリパスが混在するテキストファイルを読み込んでファイルパスだけ抽出する
import os
input_file = 'list.txt'
output_file = 'files_only.txt'
with open(input_file, 'r', encoding='utf-8') as f:
paths = f.read().splitlines()
# 実際にファイルとして存在するものだけを抽出
files_only = [p for p in paths if os.path.isfile(p)]
with open(output_file, 'w', encoding='utf-8') as f:
f.write('\n'.join(files_only))
print(f"完了: {len(files_only)} 個のファイルを抽出しました。")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment