Python: Concatenate old csv filename with extra text and save it

Question:

I’ve written below code. but it saved second csv like "15.csv+ws.csv". I want it to save it like "15ws.csv". have there any way to do it?

fname = r"15.csv"
ap= open(f"{fname} + 'ws.csv'",'a') 
ap.write(" "+r) 
ap.close()
Asked By: The Flain

||

Answers:

You can use pathlib library.

from pathlib import Path
ap= open(f"{Path(fname).stem}ws.csv",'a') 

It will save second csv with "15ws.csv" filename

Answered By: Bhagi20

See this question:

Try this:

import os

fname = r"15.csv"

name, ext = os.path.splitext(fname)
ap = open(f"{name}ws{ext}", 'a') 
ap.write(" "+r) 
ap.close()
Answered By: Bill
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.