Reputation: 81
i don't want to save the csv files manually.Is there a code that could be done automatically?First i load the file1,convert it to dataframe and do some processes that doesn't have any significant about my question. Here's the file:
file1="C:/Users/ozzgu/OneDrive/Desktop/ml articles/model/datasets/RC101.txt"
rc1=pd.read_csv(file1,sep="\s+")
After processing,i need to save txt file just like below.
vehicle1.to_csv("vehicle1_KMEANS3V_RC101.txt")
If I load file1=RC102.txt,does it have a way to save automatically "vehicle_KMEANS3V_RC102.txt".Thanks
Upvotes: 0
Views: 1213
Reputation: 303
You can use the os library to extract the file name from the path, then append the text to it, and pass it to the saving function.
import os
filepath = "C:/Users/ozzgu/OneDrive/Desktop/ml articles/model/datasets/RC101.txt"
filename = os.path.basename(filepath)
new_filename = "vehicle1_KMEANS3V_" + filename
print(f"Old filename: {filename}")
print(f"New filename: {new_filename}")
Ouput:
Old filename: RC101.txt
New filename: vehicle1_KMEANS3V_RC101.txt
Upvotes: 1