0

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
  • 131,796
  • 23
  • 122
  • 195
michaelg
  • 223
  • 1
  • 6
  • 25

1 Answers1

1

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
  • 131,796
  • 23
  • 122
  • 195