1

I have the following code f = open('01-01-2017.csv')

From f variable, I need to remove the ".csv" and set the remaining "01-01-2017" to a variable called "date". what is the best way to accomplish this

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
michaelg
  • 243
  • 2
  • 8
  • 25

1 Answers1

2

just retrieve the name of the file using f.name and apply os.path.splitext, keep the left part:

import os
date = os.path.splitext(os.path.basename(f.name))[0]

(I've used os.path.basename in case the file has an absolute path)

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219