Select to view content in your preferred language

Python script for relative path not working

6149
12
04-21-2015 09:57 AM
JasonThomas2
New Contributor II

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

12 Replies
SepheFox
Frequent Contributor

Not sure how much I helped, but you're welcome!

0 Kudos
JasonThomas2
New Contributor II

BTW, I also tried using the following two lines to clean the file path.

It worked in that the script starting working in the first folder I supplied. However once it got done with the first folder it throws an error on the line that 'for file in mxdList'

templine=line.strip()

Workspace = templine

0 Kudos
BlakeTerhune
MVP Regular Contributor
0 Kudos