0

Below is my API code where I am trying to update the file path on a daily basis to run an API call.

I am unable to determine how I can update the file name on a daily basis. Some help would be highly appreciated.

import requests

url = "*******"

payload = {'Content-Disposition': 'form-data',
           'Content-Type': 'text/plain',
           'name': 'file'}
files = [
  ('file', open('/C:/Users/SET/Desktop/TEST/TEST/test_test_test_file_20201001.csv','rb'))
]
headers = {
  'Content-Type': 'multipart/form-data',
  'X-API-TOKEN': '*******'
}

response = requests.request("POST", url, headers=headers, data = payload, files = files)

print(response.text.encode('utf8'))
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • Is it specifically the `test_test_test_file_20201001.csv` part you want to update? If so, should it be todays date? – Hampus Larsson Oct 07 '20 at 08:48
  • Yes That's the part I want to update – Aanurag Das Oct 07 '20 at 08:51
  • So basically test_ will be my regex and the rest of the file name will be updated on a daily basis – Aanurag Das Oct 07 '20 at 08:52
  • Something similar to this pattern='test_*.csv' – Aanurag Das Oct 07 '20 at 08:52
  • I understand what you want, I don't understand what exactly is the problem. Are you having problem running the script daily? Are you having trouble changing the path? How do you expect to change it? If you change, how do you know if there going to be a file with that name? – Tomerikoo Oct 07 '20 at 08:56
  • Does this answer your question? [how to create a file name with the current date & time in python?](https://stackoverflow.com/questions/10607688/how-to-create-a-file-name-with-the-current-date-time-in-python) – Tomerikoo Oct 07 '20 at 09:02
  • The problem here is that I am unable to determine how I will be able to change the file name inside the open('/C:/Users/SET/Desktop/TEST/TEST/test_test_test_file_20201001.csv','rb'). I already tried creating the file name in a variable and run it inside open() but it just looks for the file name which is available in "". I aplogize if I am not able to explain better. Still learning my way through pyhton – Aanurag Das Oct 07 '20 at 09:07
  • This is a more accurate duplicate: https://stackoverflow.com/questions/16713643/how-to-add-the-date-to-the-file-name – Tomerikoo Oct 07 '20 at 09:10
  • I figured it out.Thanks for all your help – Aanurag Das Oct 07 '20 at 09:12

1 Answers1

0

I am trying to update the file path on a daily basis

The code below will create the file name based on the current date:

from datetime import date

today = date.today()
file_name = f'test_test_test_file_{today.year}{today.month:02}{today.day:02}.csv'
print(file_name)

output

test_test_test_file_20201007.csv
balderman
  • 22,927
  • 7
  • 34
  • 52
  • 1
    I would use `strftime` instead to just extract the info you want: `datetime.date.today().strftime("%Y%m%d")` – Hampus Larsson Oct 07 '20 at 09:00
  • 1
    Or use f-strings width parameters so both the month and day is correctly formatted without the need for any ternary logic: `f'test_test_test_file_{today.year}{today.month:02}{today.day:02}.csv'` – Hampus Larsson Oct 07 '20 at 09:09