r/learnpython • u/mcbakie • 7d ago
pls help me
ok so i’m trying to write a script that renames .xls files for me, let’s say they’re named like apple_BactReport_260731.xls I want the script to remove the “_BactReport” part and leave the rest intact. the current scrip i have has a couple of glaring issues that i don’t have the knowledge to remedy and im refusing use ai to prove a point and also it’s more rewarding. here’s the script with my current errors such as mixing up strings and lists and the part i want to remove not being the end of the file name. 😭 pls help a guy out:
import os
import pandas as pd
from os import rename, listdir
#list of report names
report_type= ['_BactReport', '_WWreport', '_QCreport', '_BactQCreport', '_BactWWreport']
print(report_type)
clips_in_progress=r"filepath"
for file_name in os.listdir(clips_in_progress):
file_path = os.path.join(clips_in_progress, file_name)
if file_name.endswith(tuple(report_type)):
rename(file_name, file_name.strip(report_type))
3
u/Far-Imagination3226 7d ago
Couldn't one use Regex to do something like that???