-2

I am trying to generate a filename in python on mac to record data everyday so that the filename has date in filename. Please refer the command below.

oi_filename= os.path.join("markets","storage","oi_data_records_{0}.json".format(datetime.now().strftime(%d%m%Y)))

where markets is in Desktop and storage is in markets folder.

Error
  File "<ipython-input-20-e3a1aee3f506>", line 21
    oi_filename= os.path.join("markets","storage","oi_data_records_{0}.json".format(datetime.now().strftime(%d%m%y)))
                                                                                                            ^
SyntaxError: invalid syntax

The basic idea is everyday a file to be created with full date in name so that the rest of program can park the data in the respective file.

U13-Forward
  • 69,221
  • 14
  • 89
  • 114
  • 2
    Did you look at any of the examples? `strftime` accepts a string. A string is always enclosed in quotes. Yours is not. That's why you get a syntax error. – Tim Roberts Aug 21 '21 at 06:16
  • 2
    And seriously, you need to consider using "%y%m%d" instead. That way, your files will sort in date order. – Tim Roberts Aug 21 '21 at 06:17

2 Answers2

0

It's because you didn't put the %d%m%y in a string.

oi_filename= os.path.join("markets","storage","oi_data_records_{0}.json".format(datetime.now().strftime(%d%m%Y)))

should be:

oi_filename= os.path.join("markets","storage","oi_data_records_{0}.json".format(datetime.now().strftime("%d%m%Y")))
Daniyal Warraich
  • 446
  • 3
  • 13
0

You are missing quotation marks for strftime(%d%m%y). It should be strftime('%d%m%y')

hangonstack
  • 162
  • 5