Selecting files from multiple folders

3334
6
05-16-2012 08:19 AM
denisedavis
Occasional Contributor III
I haven't played in Python for almost two years so I'm very rusty. I'm definitely not saying I was all that good at it then. I could use some help with this.
I have a folder, that contains many folders, and each of those folders have a file I need to grab and put it into my map. I'm going to have to do this 1000's of times so I thought a script would be nice to run. Here's what I have so far (and I got help with this too):

# Import arcpy module
import arcpy
import string
import os


# Local variables:
#Set workspace
arcpy.env.workspace = "C:\\DWG\\FOLDER\\FOLDER*"
Polyline = "\Polyline"
LineShp = "C:\\DWG\\FOLDER\\Shape\\"

# Process: Select
for i in arcpy.ListFiles ("*.dwg"):
    x = i.strip('.dwgDWG')
    fileout = LineShp + x + ".shp"
    print "Processing:" + arcpy.env.workspace + "\\" + Polyline + "To:" + fileout


It looks like its running but when I go to the folder where the .shp should be, it's empty. My print statement doesn't print anything. I'm not sure I can use a wildcard for my workspace either. Thanks for your help!
Tags (2)
0 Kudos
6 Replies
BryanThom
New Contributor
By no means is this answer going to be complete, but when specifying a file location (as in C:\DWG.....) you only need to use the double backslash after the C:\\ - so it would look like "C:\\DWG\...." or you can use single forward (/) slashes instead, which admittedly is annoying if you are doing a copy/paste of an address.  another way to reference file paths is to put an r in front of it, so like: r"C:\DWG\..."

I am diving into the wonderful world of python myself for the first time in 3 years, and the first time using it with arcmap.  There is a free online course you can take for python in the training section - it did a lot to get me started.  I am sure someone will be able to provide a better and more complete answer but this should at least get you started.
0 Kudos
MathewCoyle
Frequent Contributor
Try posting your entire code. All I see it doing is print this line for every iteration of your loop
    print "Processing:" + arcpy.env.workspace + "\\" + Polyline + "To:" + fileout
0 Kudos
denisedavis
Occasional Contributor III
Try posting your entire code. All I see it doing is print this line for every iteration of your loop

This is all I have so far but I see what you mean. I want the shapefiles to go into the folder C:\DWG\FOLDER\Shape. Like I said, I need some help. I'm going to be a little clearer here:
1) Open folder containing many folders
2) Open each folder within the folder and get the polyline file from the dwg contained in each folder
3) I'd like each polyline to go straight into my map but I haven't figured that out yet so:
4) make a shapefile of each polyline from each dwg that came from each folder
5) I also have a query I'd like to place in the properties of each polyline from each dwg but I haven't figured that out yet either

I thank you for your time and any help you can provide.
0 Kudos
denisedavis
Occasional Contributor III
By no means is this answer going to be complete, but when specifying a file location (as in C:\DWG.....) you only need to use the double backslash after the C:\\ - so it would look like "C:\\DWG\...." or you can use single forward (/) slashes instead, which admittedly is annoying if you are doing a copy/paste of an address.  another way to reference file paths is to put an r in front of it, so like: r"C:\DWG\... ["/QUOTE]
Thanks for reminding me of this. I had forgotten.

RPI_Bryan;199036 wrote:
There is a free online course you can take for python in the training section.

I did this about 2 years ago but I should probably run through it again.
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Hi Denise,

Here is some sample code that should get you started.  What you will need to do is use the 'os.walk' command to iterate through all sub-directories.  The below code starts at the directory specified with 'env.workspace' and iterates through all of its sub-directories.  It will add the polyline file of any DWG it finds.

import arcpy, os
from arcpy import env

env.workspace = r"C:\temp\python"


for (path, dirs, files) in os.walk(env.workspace):
    for file in files:
        # find all DWG files in folder and all subfolders
        if ".dwg" in file.lower()[-4:]:
            dwg_path = os.path.join(path, file) + "\Polyline"
            # set the MXD
            mxd = arcpy.mapping.MapDocument("CURRENT")
            # set the Dataframe
            df = arcpy.mapping.ListDataFrames(mxd,"*")[0]
            # create a new layer
            newlayer = arcpy.mapping.Layer(dwg_path)
            # add the layer to the map
            arcpy.mapping.AddLayer(df, newlayer,"AUTO_ARRANGE")

del mxd
0 Kudos
denisedavis
Occasional Contributor III
Thanks Jake! It's a great start and a whole lot of help! It's crashing after 4 iterations so I have to look at the dwg files. For some reason, I'm getting an error (no arcpy module found) when I try to run it in IDLE.
0 Kudos