sammy
sammy

Reputation: 3

unable to get a variable into a list

I have the following code however, the last "_36.0_sumoprce.txt" is the only one getting date, none of the other ones.

dates = time.strftime("%Y%m%d")
files_to_remove = ("{!s}_23.0_sumoocop.txt","{!s}_36.0_sumoprce.txt", "{!s}_35.0_sumoeprc.txt".format(dates, dates, dates))

print(files_to_remove)

What I currently see:

('{!s}_23.0_sumoocop.txt', '{!s}_36.0_sumoprce.txt', '20141218_35.0_sumoeprc.txt')

What it should print:

('20141218_23.0_sumoocop.txt', '20141218_36.0_sumoprce.txt', '20141218_35.0_sumoeprc.txt')

Any any idea what I'm doing wrong?

Upvotes: 0

Views: 43

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1124828

You need to call str.format() on each of the strings separately; you are not calling it on the first two strings:

files_to_remove = (
    "{!s}_23.0_sumoocop.txt".format(dates),
    "{!s}_36.0_sumoprce.txt".format(dates),
    "{!s}_35.0_sumoeprc.txt".format(dates)
)

or you can use a list compreshension:

files_to_remove = ("{!s}_23.0_sumoocop.txt", "{!s}_36.0_sumoprce.txt", "{!s}_35.0_sumoeprc.txt")
files_to_remove = [s.format(dates) for s in files_to_remove]

Note that !s entirely redundant here because dates is already a string object.

Upvotes: 2

Related Questions