Pass a variable to a path inside a loop

185
3
Jump to solution
09-06-2017 09:07 AM
DuminduJayasekera
New Contributor III

Hi,

I need to pass year to the path inside a for loop in python script. 

years = [2010,2011,2012]
months = [9]

for year in years:
   for month in months:
     arcpy.env.workspace = r'H:\Test_ONLY\prism_800m_'+ year[:4]
     scratch = r'H:\Test_ONLY\scratch'
     if year % 4 > 0 and month == 9:
          fmaskrasters1 = fmaskrasters[243:249]
          fmaskrasters2 = fmaskrasters[249:255]
          fmaskrasters3 = fmaskrasters[255:261]
          fmaskrasters4 = fmaskrasters[261:267]
          fmaskrasters5 = fmaskrasters[267:273]
     else:
          print("Not configured in the script!")
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

I want to change r'H:\Test_ONLY\prism_800m_2010 each time for the months from Sep. 

How can I achieve this by modifying the above code. I think I am not doing something right in arcpy.env.workspace = r'H:\Test_ONLY\prism_800m_'+ year[:4]

can somebody help me to correct this?

Thanks in advance.

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor

Hi Dimindu,

Try the following:

arcpy.env.workspace = r'H:\Test_ONLY\prism_800m_'+ str(year)

View solution in original post

3 Replies
JakeSkinner
Esri Esteemed Contributor

Hi Dimindu,

Try the following:

arcpy.env.workspace = r'H:\Test_ONLY\prism_800m_'+ str(year)

DuminduJayasekera
New Contributor III

Thanks a lot. It worked.

0 Kudos
RebeccaStrauch__GISP
MVP Esteemed Contributor

Don't forget to mark Jake's answer correct so the thread can be closed. Thanks.

0 Kudos