私は次のことをしようとしています:
- ファイルから日付を読み取ります (日付の形式は %Y-%m-%d です)
- 文字列をdatetime objに変換します(私はstrptimeでこれをやっています)
- 日時の前日を取得する
- 前日 (look_back) を指定された形式の文字列に変換します
手順 1 と 2 は問題ではありません。次のように実行します。
import datetime
from datetime import timedelta
import time
now = datetime.datetime.now() #checks current datetime
### READ LAST RUN DATETIME ###
try:
test = open("last_settings.ini", "r") #opens file in read mode
print "Name of the file: ", test.name
last_run = test.read(10); # just reads the date
print "Program was last run: %s" % last_run
test.close()
firstRun = 'False'
except:
print "Settings file does not exist"
#test = open("last_settings.ini", "w+") #creates the file
#test.close()
#first_run = 'True'
#look_back = str(now-timedelta(days=14)) #sets initial lookBack date of two weeks
#print "Pulling down all paid invoices from date " + look_back
### 24 hour lookback ###
look_back = time.strptime(last_run, "%Y-%m-%d")
ただし、特定の日付より前の日付を取得するために試みたすべての方法 (上記の #3) はエラーをスローします。私のコード:
look_back = look_back-timedelta(days=1)
エラー:
look_back = look_back-timedelta(days=1)
TypeError: unsupported operand type(s) for -: 'time.struct_time' and 'datetime.timedelta'
これを行う方法についてのアイデアはありますか?