Answer the question
In order to leave comments, you need to log in
How to insert a variable into os.system?
Good day. Tell me how to correctly insert a variable into the os.system command.
The script makes a monthly archive of backups and cleans the directory with backups. I get the feeling that this is not the prettiest solution. I had to introduce an extra variable arch_name in order to insert the month number into the archive name. The script is executed on the first day of the month, so you have to subtract one from the ordinal number of months.
import os
import datetime
now_date = datetime.date.today()
month = now_date.month
if int(month) - 1 != 0: #Проверка янв или нет
t = int(month) - 1
else:
t = 12
os.chdir('/backup')
arch_name = ('tar cvf /backup_month/' + str(t) + '.tar.gz * && rm *')
os.system(arch_name)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question