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
It's hard to tell, but do you have your inside loop indented within your outside loop?
Sorry - when I pasted in the code the tabs showed up as only single spaces. I typed in the tabs manually in the forum box this time so they show up better.
import arcpy
import os
import sys
os.chdir("u:\jthomas")
f=open('testpaths.txt','r')
for line in f.xreadlines():
Workspace = line
arcpy.env.workspace = Workspace
print Workspace
mxdList = arcpy.ListFiles("*.mxd")
for fileobj in mxdList:
filepath=os.path.join(Workspace, fileobj)
print filepath
mxd=arcpy.mapping.MapDocument(filepath)
mxd.relativePaths = True
mxd.save()
print line
f.close()
Thanks - I just tried your suggestion. I inserted the print mxdList right after the line that sets up mxdList. It is printing a blank a line and the text, [] for each folder until it gets to the last folder. At that point it prints a list of file names (not full paths) inside the square brackets (see below).
[u'file1.mxd', u'file.mxd', u'file.mxd']
So, it seems that it isn't getting a proper execution of the .ListFiles method until it hits the end of the folders in the input file.
try putting your path like this: r"u:\jthomas"
You mean inside the text file that has the lists of files?
Yes, it you have the path before each one. The reason I'm thinking this is an issue, is the u in your list of file names.
Thanks. I tried that fix to the input file. I got the output below:
r"G:\Clients\Liberty Groves - 1824\18240701-Liberty Groves\GIS\Map\Output_to_Hogle"
None
Runtime error
Traceback (most recent call last):
File "<string>", line 14, in <module>
TypeError: 'NoneType' object is not iterable
I think I found the solution. The combination of the strip method and NOT having a "/" at the end of each folder path in the list (input file) made it work in all the folders this time.
Here is the final code.
import arcpy
import os
import sys
os.chdir("u:\jthomas")
f=open('testpaths.txt','r')
for line in f.xreadlines():
#this next line appears to fix the problem of not entering first folders
templine=line.strip()
Workspace = templine
arcpy.env.workspace = Workspace
print Workspace
mxdList = arcpy.ListFiles("*.mxd")
print mxdList
for file in mxdList:
filepath=os.path.join(Workspace, file)
print filepath
mxd=arcpy.mapping.MapDocument(filepath)
mxd.relativePaths = True
mxd.save()
print line
f.close()
input file format below
g:\clientA\project1\GIS\Map
Thanks For your help working through this Sephe Fox! I appreciate your help