0

I'm running into some difficulties with python. I have a code I'm using in conjunction with ArcGIS that is parsing filenames into a database to return the corresponding unique ID and to rename the folder with this unique ID. It has been working great before, but I need to handle some exceptions, like when the Unique ID already exists within the directory, and when the action has already been completed on the directory. The unique id contains all numbers so I've been trying:

elif re.findall('[0-9]', fn):
 Roll = string.join(string, "1")
 print (Roll)
    os.rename(os.path.join(basedir, fn),
                os.path.join(basedir, Roll))

which returns all folders with a unique ID. I just can't figure out how to get a count of the number of times a specific folder name occurs in the directory.

GISHuman
  • 1,026
  • 1
  • 8
  • 27
  • 1
    Why do you need a count? A folder name can exist at most once in a given directory. Use `os.path.isdir()` or `os.path.exists()`. – roippi Jul 18 '13 at 16:17
  • Sorry to clarify, if a folder ALREADY exists and the existing script outputs the same name I would like to rename the duplicate folder with a new name like say this was a duplicate" 923449039" so that the second instance would be "923449039_1" – GISHuman Jul 18 '13 at 17:03

3 Answers3

1

add the name to a set and then check if it's in the set.

Jiminion
  • 5,080
  • 1
  • 31
  • 54
1

I suspect you're making this way harder on yourself than you need to, but answering your immediate question:

folder_name_to_create = 'whatever'

if os.path.exists(folder_name_to_create):
    folder_name_to_create += '_1'

If you are getting name collisions, I suspect you need to look at your "unique" naming algorithm, but maybe I'm misunderstanding what you mean by that.

roippi
  • 25,533
  • 4
  • 48
  • 73
  • Basically someone created a directory of folders associated with properties and named them as addresses. In our organization, everything is done by a unique ID called a Roll number. So basically I'm taking the folder name , running it through our database to get the roll # and renaming that folder, however there may be SOME duplicates. – GISHuman Jul 18 '13 at 18:05
  • @GISKid Sounds like your database needs some unique constraints as well. – Keith Jul 18 '13 at 19:09
0

One way to do it might be the following: Create a dictionary whose keys are your folder names, and the value associated with each key is an integer, the number of occurrences of that name. Each time you process a folder, update the dictionary's keys/values appropriately. After you've added all the folder names in your set, check all the count values in the dictionary, and any time the count is > 1 you know you have a duplicate.

Or, if you need to detect duplicates as you go, just check whether the key already exists. In that case you don't really need the value at all, and you can use a set or list instead of a dict.

You could use collections.Counter to help you in this. You can see an example usage in this question. It shouldn't be too difficult to adapt that example to your needs.

Hope this helps.

Community
  • 1
  • 1
DMH
  • 3,875
  • 2
  • 26
  • 25