0

I'm stuck. I'm moving folders around on our network which all have a unique ID into a central location. There are a few folders with typos and therefore do not match a unique ID in the central location. I have found the correct IDS but I need to rename these folders before I move them. For example, I have created an excel spreadsheet with the wrong unique ID and in a separate column have the correct ID. Now, I want to rename the folders with the correct ID and then transfer those folders to the central location. My code is....rough because I can't think of a good way to do it. I feel like using a list is the way to go, but since my code is iterating through a folder I'm not sure how to achieve this

Edit: I think something like this may be what I'm looking for

Ex: In Folder A : A file named 12334 SHOULD be renamed 1234. Then moved to the base directory with in folder 1234.

Heres my code:

import os
import re
import sys
import traceback
import collections
import shutil

movdir = r"C:\Scans"
basedir = r"C:\Links"
subfolder = "\Private Drain Connections"

try:
    #Walk through all files in the directory that contains the files to copy
    for root, dirs, files in os.walk(movdir):
        for filename in files:
            #find the name location and name of files
            path = os.path.join(root, filename)

            #file name and extension
            ARN, extension = os.path.splitext(filename)
            print ARN

            #Location of the corresponding folder in the new directory
            link = os.path.join(basedir, ARN)
            if not os.path.exists(link):
                newname = re.sub(372911000002001,372911000003100,ARN)
                newname =re.sub(372809000001400,372909000001400,ARN)
                newname =re.sub(372809000001500,372909000001500,ARN)
                newname =re.sub(372809000001700,372909000001700,ARN)
                newname = re.sub(372812000006800,372912000006800,ARN)
                newname =re.sub(372812000006900,372912000006900,ARN)
                newname =re.sub(372812000007000,372912000007000,ARN)
                newname =re.sub(372812000007100,372912000007100,ARN)
                newname =re.sub(372812000007200,372912000007200,ARN)
                newname =re.sub(372812000007300,372912000007300,ARN)
                newname =re.sub(372812000007400,372912000007400,ARN)
                newname =re.sub(372812000007500,372912000007500,ARN)
                newname =re.sub(372812000007600,372912000007600,ARN)
                newname =re.sub(372812000007700,372912000007700,ARN)
                newname =re.sub(372812000011100,372912000011100,ARN)


                os.rename(os.path.join(movdir, ARN, extension ),
                os.path.join(movdir, newname, extension))
                oldpath = os.path.join(root, newname)
                print ARN, "to", newname
                newpath = basedir + "\\" + newname + subfolder
                shutil.copy(oldpath, newpath)
                print "Copied"

except:
    print ("Error occurred")

Thanks to the answers below here is my final code:

import arcpy
import os
import re
import sys
import traceback
import collections
import shutil

movdir = r"C:\Scans"
basedir = r"C:\Links"
subfolder = "\Private Drain Connections"


import string

l = ['372911000002001',
'372809000001400',
'372809000001500',
'372809000001700',
'37292200000800'
]
l2 = ['372911000003100',
'372909000001400',
'372909000001500',
'372909000001700',
'372922000000800'
]

try:
    #Walk through all files in the directory that contains the files to copy
    for root, dirs, files in os.walk(movdir):
        for filename in files:
            #find the name location and name of files
            path = os.path.join(root, filename)

            #file name and extension
            ARN, extension = os.path.splitext(filename)
            oldname = str(ARN)
            #Location of the corresponding folder in the new directory
            link = os.path.join(basedir, ARN)

            if not os.path.exists(link):
                for ii, jj in zip(l, l2):
                    newname = re.sub(ii,jj, ARN)
                    newname = str(newname)
                    print path
                    newpath = os.path.join(root, oldname) +  extension
                    print "new name", newpath
                    os.rename(path, newpath)
                    print "Renaming"
                    newpath2 = basedir + "\\" + newname + subfolder
                    shutil.copy(newpath, newpath2)
                    print "Copied"
                if newname != ARN:
                    break


            else:
                continue

except:
    print ("Error occurred")
    tb = sys.exc_info()[2]
    tbinfo = traceback.format_tb(tb)[0]
    pymsg = "PYTHON ERRORS:\nTraceback Info:\n" + tbinfo + "\nError Info:\n    " + \
        str(sys.exc_type)+ ": " + str(sys.exc_value) + "\n"
    msgs = "GP ERRORS:\n" + arcpy.GetMessages(2 )+ "\n"
    print (pymsg)
    print (msgs)
6
  • 2
    That's... quite a bit more than just replacing a list... Commented Aug 23, 2013 at 14:40
  • @IgnacioVazquez-Abrams I have the other part of the code down and working , I just need a more efficient way of replacing the bad names. Commented Aug 23, 2013 at 14:45
  • Efficient or elegant? How many wrong folders are we talking about? Commented Aug 23, 2013 at 15:07
  • @Jblasco there's only 30 currently Commented Aug 23, 2013 at 15:11
  • Then I don't think efficiency will be an issue... Elegance takes precedence in my opinion ;) Commented Aug 23, 2013 at 15:16

2 Answers 2

1

For me the way to go is to read both lists into list objects:

list1 = ["372911000002001", "372809000001400", "372809000001500"]
list2 = ["372911000003100", "372909000001400", "372909000001500"]
for ii, jj in zip(list1, list2):
    newname = re.sub(ii,jj,ARN)  #re.sub returns ARN if no substitution done
    if newname != ARN:
       break
Sign up to request clarification or add additional context in comments.

Comments

0

An idea: try to convert the id's to strings. I mean:

            newname = re.sub('372911000002001','372911000003100',ARN)

Hope it helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.