I have an entire data folder ending with .csv.
All csv files are named like this :
- "data_AA_10_344362.csv"
- "data_AA_25_124567.csv"
- "data_AA_37_896432.csv"
- etc.
I want to rename all of them by removing everything after the last underscore but keep '.csv'. So i would like my files to be renamed like this automatically :
- "data_AA_10.csv"
- "data_AA_25.csv"
- "data_AA_37.csv"
I've tried something like this but it's not complete/incorrect :
import glob, os
import pathlib
DATA_DIR = pathlib.Path('C:\\Users\\dvs\\documents\\data')
for old in DATA_DIR.glob('*.csv'):
new = '_'.join(old.split('_')[:-1])
os.rename(old, new)
I know this question has already been answered but i couldn't manage to do it on my own.
CodePudding user response:
You can use:
DATA_DIR = pathlib.Path('C:\\Users\\dvs\\documents\\data')
for old in DATA_DIR.glob('*.csv'):
new = old.parent / f"{'_'.join(old.stem.split('_')[:-1])}{old.suffix}"
old.rename(new)
print(f'Renamed {old} to {new}')
Output (my data folder is 'cities')
Renamed cities/data_AA_25_124567.csv to cities/data_AA_25.csv
Renamed cities/data_AA_10_344362.csv to cities/data_AA_10.csv
Renamed cities/data_AA_37_896432.csv to cities/data_AA_37.csv
CodePudding user response:
Using .rename from pathlib:
from pathlib import Path
path = "C:\\Users\\dvs\\documents\\data"
[x.rename(f"{path}/{x.stem.rsplit('_', 1)[0]}.csv") for x in Path(path).rglob("*.csv")]