AnsweredAssumed Answered

Python script for relative path not working

Question asked by EndlessTrail on Apr 21, 2015
Latest reply on Apr 21, 2015 by blake.terhune

I have a short script that reads a text file with a list of folders. For each folder it cycles through every MXD file and sets them to relative paths. For some reason the result of running this python script in the ArcCatalog command line is it only set MXDs to relative path in the last folder on my list. If my input list contains three file folders it only works on files in the third folder. If my input list contains 4 file folders it only works on those in the fourth folder. I added some Print commands to keep watch on the Workspace and filepath variables. This tells me that it correctly obtains the folder name every time but doesn't see the MXDs in the first folders. It appears that the inside loop is not running at all until it gets to the last iteration of the outside loop.

 

I know I'm close to getting this to work but I am currently frustrated and would really appreciate your input!

 

import arcpy
import os
import sys

os.chdir("u:\jthomas")

f=open('testpaths2.txt','r')

# xreadlines methods used to access one row/line at a time from large text list of folder names

for line in f.xreadlines():
Workspace = line
arcpy.env.workspace = Workspace
#print command used to watch current folder value

print Workspace
mxdList = arcpy.ListFiles ("*.mxd")
for fileobj in mxdList:
  filepath=os.path.join(Workspace, fileobj)
  #print command used to watch filepath variable
  print filepath
  mxd=arcpy.mapping.MapDocument(filepath)
  mxd.relativePaths = True
  mxd.save()
#print command used to watch line value

print line
f.close()

 

The file 'testpaths2.txt' is a list of filepaths. Each filepath is followed by a hard return.

 

Thanks,

 

-j-

 

Jason Thomas

Outcomes