Home > database >  how to print csv with the same pathname but with an extension?
how to print csv with the same pathname but with an extension?

Time:02-05

In the code that I present, it reads csv files that are in one folder and prints them in another, I want this print to be with the same name that it has in the path but with an extension. For example, if the file is called: aaa.csv, the print would be aaa_ext.csv the print i get are file_list0.csv, file_list1.csv, file_list2.csv

This is my code:

import pandas as pd   
import numpy as np       
import glob   
import os  
all_files = glob.glob("C:/Users/Gamer/Documents/Colbun/Saturn/*.csv")   


file_list = []   
for i,f in enumerate(all_files):   
    df = pd.read_csv(f,header=0,usecols=["t","f"])
    df.to_csv(f'C:/Users/Gamer/Documents/Colbun/Saturn2/file_list{i}.csv') 

CodePudding user response:

you can modify the line that writes the csv file as follows:

df.to_csv(f'C:/Users/Gamer/Documents/Colbun/Saturn2/{os.path.basename(f).split(".")[0]}_ext.csv') 
  • Related