if f.endswith(mxd_match): mxd = arcpy.mapping.MapDocument(full_path) lyrs = arcpy.mapping.ListLayers(mxd) for lyr in lyrs: try: ..... del lyrs
import os, sys, string, arcpy mxd_match = ".mxd" Directory_Search = r"\\server00\e$\restore5\Experiment\Test_10" new_connPrefix = r"C:\Documents and Settings\Application Data\ESRI\Desktop10.0\ArcCatalog" def Conn_Info(usr): if usr == "user01": new_connection = new_connPrefix + "\\" + usr + "_dir_conn@production.sde" return new_connection elif usr == "user02": new_connection = new_connPrefix + "\\" + usr + "_dir_conn@development.sde" return new_connection elif usr == "user03": new_connection = new_connPrefix + "\\" + usr + "_dir_conn@production.sde" return new_connection del new_connection for root, dirs, files in os.walk(Directory_Search): # for root, dirs, files in os.walk(arcpy.GetParameterAsText(0)): fListLength = len(files) if (fListLength != 0): n = 0 for f in files: full_path = root + "\\" + str(f) if f.endswith(mxd_match): mxd = arcpy.mapping.MapDocument(full_path) for lyr in arcpy.mapping.ListLayers(mxd): try: if lyr.supports("DATASOURCE"): if lyr.supports("SERVICEPROPERTIES"): servProp = lyr.serviceProperties user = str(servProp.get('UserName', 'N/A')) new_conn = Conn_Info(user) lyr.replaceDataSource(new_conn, "SDE_WORKSPACE", lyr.datasetName) except: print arcpy.GetMessages(2) del lyr try: # mxd.saveACopy(root + "\\" + f[:-4] + "_New.mxd", "9.3") mxd.save() except: print arcpy.GetMessages(2) del mxd del full_path
Chris:Why do I need to worry about the number of subprocesses? I wrote my main python script to loop through a directory, its subdirectories and files looking for mxds. Then I call the subprocess python script for the file that has focus and I manipulate that mxd. Once that is done and the mxd gets saved, I thought the code goes back to the main python script and releases the subprocess python script from memory. Then I loop to the next mxd and call the subprocess python script again.The reason I went this route in the first place was due to memory usage always climbing while looping through a directory until the python script just hung. Could it be that I just needed to release additional items from temporary memory when I was just using one script to obtain the results?Thanks.
import os, sys, string, arcpy import subprocess mxd_match = ".mxd" fileErrList = [] procs = [] Directory_Search = r"\\tgapparc00\e$\restore5\Planning_Run\Broken_Links\Test" for root, dirs, files in os.walk(Directory_Search): fListLength = len(files) if (fListLength != 0): n = 0 for f in files: if f.endswith(mxd_match): full_path = root + "\\" + str(f) cmd = "'C:\Python26\ArcGIS10.0\python.exe \\\\tgapparc01\\c$\\ReplaceSource.py' full_path" proc = subprocess.Popen(cmd,shell=True) procs.append(proc)
Chris:I did see your last response and I moved the python script, that is called, to a folder with no spaces. No matter how I format the cmd line, the subprocess never gets executed.
Chris:Can you please be a little bit more specific in what your code is looking for with the cmd = "python foo.py arg1 arg2" line? I still cannot get my code to evaluate the subprocess python script.Does python refer to python.exe that has been installed on the computer that you are running this script from (e.g. c:\Python26\ArcGIS10.0\python.exe)?Does foo.py refer tp the python script that you are running in the subprocess? If so, wouldn't you need to specify the full path to this file?Are arg1 and arg2 variables from the primary script that you are sending to the subprocess script? If so, what is the syntax to get the subprocess script to accept these arguments? I ask because I am using arcpy.GetParameterAsText(0) to evaluate the single argument that I am trying to pass.Your assistance is greatly appreciated. Thanks.
Chris:I tried running the first portion of your code, but the subprocess is not executing.Here is my syntax to call the subprocess script:cmd = "C:\\Python26\\ArcGIS10.0\\python.exe \\\\tgapparc01\\c$\\GIS Testing\\ReplaceSource.py full_path"In the cmd line, full_path is the file that has the focus. File is stored in the full_path variable.In the ReplaceSource.py script, I am trying to use the arcpy.GetParameterAsText(0) to bring in this argument. I'm not sure if this is the problem?Could this issue possibly be how I am calling out the python executable and the python script that gets run in the subprocess?Your assistance is greatly appreciated. Thanks.
import subprocess procs = [] cmd = "python foo.py arg1 arg2" proc = subprocess.Popen(cmd,shell=True) procs.append(proc)
def numRunningProcs(procs): numProcs = 0 for proc in procs: proc.poll() if proc.returncode == None: numProcs = numProcs + 1 return numProcs
Les membres connectés peuvent publier, suivre les mises à jour, et plus encore. Nouveau ici ? Inscrivez-vous gratuitement.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.