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()
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
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()
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()
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
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()