Select to view content in your preferred language

Script to Copy File in Modelbuilder

5644
11
Jump to solution
04-08-2016 03:07 PM
ChrisHolmes
Frequent Contributor

Hi all,

I have a very simple python script to copy a file from one location to another:

import shutil

shutil.copy("c:/chris/Test1/CitizenComments.mxd" , "c:/chris/test2/CitizenComments.mxd")

If I take this script and put it in a model builder model, what change would I have to make to the code so that I could have a parameter in the model where a user could enter an index string to be appended to the front of the file name?

Thanks!

Chrs

0 Kudos
11 Replies
DarrenWiens2
MVP Honored Contributor

The most straightforward way would be something like the following, although you may run into trouble with /" (untested):

import shutil
index = arcpy.GetParameterAsText(0) # get the first input parameter entered into the tool dialog
shutil.copy("c:/chris/Test1/CitizenComments.mxd" , "c:/chris/test2/" + index + "CitizenComments.mxd")

To better work with paths, use os.path.join:

import shutil, os
index = arcpy.GetParameterAsText(0) # get the first input parameter entered into the tool dialog
shutil.copy("c:/chris/Test1/CitizenComments.mxd" , os.path.join("c:/chris/test2", index + "CitizenComments.mxd")
ChrisHolmes
Frequent Contributor

Thanks Darren. I'll play around with your suggestions.

Have a good weekend.

Chris

0 Kudos