LoginSignup
0
0

指定したディレクトリ内に存在しているcsvファイルを一括で削除

Posted at

はじめに

ディレクトリ内に保存されているcsvファイルを一括で削除する必要があったので、備忘録として。
title.py
import os
import glob

# 指定したディレクトリ
dir_path = '/path/to/your/directory'

# ディレクトリ内のすべてのCSVファイルを取得
csv_files = glob.glob(os.path.join(dir_path, '*.csv'))

# 各CSVファイルを削除
for file in csv_files:
    os.remove(file)

補足

このコードは、指定したディレクトリ内のすべてのCSVファイルを検索し、それらを削除します。

glob.globはワイルドカードを使用してファイルパスを検索する関数で、os.path.join(dir_path, '*.csv')により指定したディレクトリ内のすべてのCSVファイルを検索します。

その後、os.removeを使用して各ファイルを削除します。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0