Export to PDF and Save a Copy saves to the wrong folder

3377
5
08-24-2011 11:23 AM
deleted-user-MS0lE1F_0-sy
Occasional Contributor
I am using parameters to save an mxd and export a pdf.  Tried using both workspace and folder.  Problem is that it gets saved to one folder down from the one I tell it two.  It also adds the name of the folder I tell I wanted it in at the front of the mxd and pdf.  What am I doing wrong.  This is my very first script!



# 8/16/2011
# Aerial Map Script

# import arcpy.
import arcpy.mapping

# Define Map Document location.
mxd = arcpy.mapping.MapDocument("C:/GIS/Script1/maps/Template_Aerial_Imagery4.mxd")

# Define Data Frame.
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]

# Add a Parameter for Locating the Layer you want to Add and Add the Layer
Layer = arcpy.GetParameterAsText(0)
addLayer = arcpy.mapping.Layer(Layer)
arcpy.mapping.AddLayer(df,addLayer,"TOP")

# Select Layer, Zoom to Layer.
arcpy.SelectLayerByAttribute_management(addLayer,"NEW_SELECTION")
df.zoomToSelectedFeatures()

# Define scale of dataframe and set the reference scale.
df.scale = 2400
df.referenceScale = 2400

# Clear the Selection
arcpy.SelectLayerByAttribute_management(addLayer,"CLEAR_SELECTION")

# Change "Project Name (PN)" & "County, State (CS)" Text; Using Parameters
ALPname = arcpy.GetParameterAsText(1)
elPN = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","Project Name")[0]
elPN.text = ALPname
CSname = arcpy.GetParameterAsText(2)
elCS = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","County, State")[0]
elCS.text = CSname

# Refresh the Active View
arcpy.RefreshActiveView

# save mxd
ProjNum = arcpy.GetParameterAsText(3)
mxd.saveACopy(arcpy.GetParameterAsText(4) + ProjNum + "_Aerial_Imagery.mxd")

# Export to PDF
PDFloc = arcpy.GetParameterAsText(5)
arcpy.mapping.ExportToPDF(mxd,PDFloc + ProjNum + "_Aerial_Imagery.pdf")

# Clear the mxd from script lock
del mxd

_____________________________________________________________________________

Also I tried to trouble shoot the problem by exporting a random map in ArcMap using python command line and it althought the name of the file was correct it also saved it a folder down one in the list.

Example.
>>> import arcpy
>>> import arcpy.mapping
>>> mxd = arcpy.mapping.MapDocument("CURRENT")
>>> arcpy.env.workspace = "C:\GIS\test\New_Folder"
>>> arcpy.mapping.ExportToPDF(mxd,"output.pdf")

It will export it to the test folder instead of the New_Folder folder.  ???
Tags (2)
0 Kudos
5 Replies
deleted-user-MS0lE1F_0-sy
Occasional Contributor
I got it figured out. In case someone else runs into this....I needed to put a "/" after the GetParameter.  I thought I had tried that, so simple.
0 Kudos
AndrewKlimek
New Contributor II

I'm having the same issue with my output file (a txt file in my case) getting outputted to the wrong folder in the directory path.  I have tried to follow Matthew's solution above to incorporate the "/" after the GetParameter, but I end up with syntax errors.  Could someone alter his code to show me where the "/" should be placed?  Thanks.

0 Kudos
deleted-user-MS0lE1F_0-sy
Occasional Contributor

Without seeing the code and error it's hard to say.  Are you putting "/" for the rest of the path? If you use "\" you need to add the r at the beginning of the path."Y:/folder1/folder2/folder3/PARAMETER INPUT"      or    r"Y:\folder1\folder2\folder3\PARAMETER INPUT" .   Is your output setting of the parameter a text?

0 Kudos
XanderBakker
Esri Esteemed Contributor

It would be better to use the os.path.join to create the path to the output file.

Example:

import os
my_folder = r"C:\aFolder\aSubFolder"
my_filename = "someFileName.ext"
print os.path.join(my_folder, my_filename)

... will yield this:

C:\aFolder\aSubFolder\someFileName.ext

And you can even do this:

import os
my_folder = r"C:\aFolder\aSubFolder"
my_filename = "someFileName.ext"
print os.path.join(my_folder, "anotherSubFolder", my_filename)

... which will yield this:

C:\aFolder\aSubFolder\anotherSubFolder\someFileName.ext
0 Kudos
AndrewKlimek
New Contributor II

Thank you Matthew and Xander.

Using the os.path.join worked like a charm.

0 Kudos