Код: Выделить всё
MYSTRING = f"filepath/{year}/{month}"
Этот код относится к 2023 году, как и ожидалось:
Код: Выделить всё
last_n_months = 12
months = date.today().year * 12 + date.today().month - 1 # Months since year 0 minus 1
tuples = [((months - i) // 12, (months - i) % 12 + 1) for i in range(last_n_months)]
for item in tuples:
year = str(item[0])
month = str(item[1])
# string used in directly with the format() function
print(f"xyz/Year={year}/Month={month}".format())
------------------------
xyz/Year=2024/Month=11
xyz/Year=2024/Month=10
xyz/Year=2024/Month=9
xyz/Year=2024/Month=8
xyz/Year=2024/Month=7
xyz/Year=2024/Month=6
xyz/Year=2024/Month=5
xyz/Year=2024/Month=4
xyz/Year=2024/Month=3
xyz/Year=2024/Month=2
xyz/Year=2024/Month=1
xyz/Year=2023/Month=12
Код: Выделить всё
# the format string
myformatstring = f"xyz/Year={year}/Month={month}"
last_n_months = 12
months = date.today().year * 12 + date.today().month - 1 # Months since year 0 minus 1
tuples = [((months - i) // 12, (months - i) % 12 + 1) for i in range(last_n_months)]
for item in tuples:
year = str(item[0])
month = str(item[1])
# use the format string variable from outside the loop
print(myformatstring.format())
------------------------
xyz/Year=2023/Month=12
xyz/Year=2023/Month=12
xyz/Year=2023/Month=12
xyz/Year=2023/Month=12
xyz/Year=2023/Month=12
xyz/Year=2023/Month=12
xyz/Year=2023/Month=12
xyz/Year=2023/Month=12
xyz/Year=2023/Month=12
xyz/Year=2023/Month=12
xyz/Year=2023/Month=12
xyz/Year=2023/Month=12
Подробнее здесь: https://stackoverflow.com/questions/792 ... de-of-loop
Мобильная версия