1

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()
The Flain
  • 41
  • 4
  • 1
    Does this answer your question? [How to add an id to filename before extension?](https://stackoverflow.com/questions/37487758/how-to-add-an-id-to-filename-before-extension) – Bill Feb 12 '23 at 07:03

2 Answers2

2

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()
Bill
  • 10,323
  • 10
  • 62
  • 85
1

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

Bhagi20
  • 98
  • 5