Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #25653

Re: Finding duplicate file names and modifying them based on elements of the path

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'paths': 0.05; 'subject:file': 0.07; 'basename': 0.09; 'collections': 0.09; 'dict': 0.09; 'dirname': 0.09; 'keyed': 0.09; 'os.path': 0.09; 'prepended': 0.09; 'solution,': 0.09; 'def': 0.10; 'skip:f 30': 0.15; 'client,': 0.16; 'collapse': 0.16; 'defaultdict': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'new_path': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'renaming': 0.16; 'subject: \n ': 0.16; 'subject:based': 0.16; 'wrote:': 0.17; 'duplicate': 0.17; 'skip:{ 20': 0.17; 'thanks,': 0.18; 'skip:" 30': 0.20; 'skip:" 40': 0.20; 'sort': 0.21; 'import': 0.21; 'header:In-Reply-To:1': 0.25; 'header:User- Agent:1': 0.26; 'creating': 0.26; 'entries': 0.27; 'tree': 0.27; 'reply.': 0.28; 'behavior.': 0.29; 'index,': 0.29; 'mind,': 0.29; 'received:192.168.1.3': 0.29; 'unique,': 0.29; 'this.': 0.29; 'relative': 0.30; 'file': 0.32; 'received:84': 0.32; 'structure': 0.32; 'skip:s 30': 0.33; 'directory,': 0.33; 'to:addr:python- list': 0.33; 'another': 0.33; 'entry': 0.33; "can't": 0.34; 'thanks': 0.34; 'list': 0.35; 'path': 0.35; 'especially': 0.35; 'doing': 0.35; 'pm,': 0.35; 'something': 0.35; 'there': 0.35; 'but': 0.36; 'client': 0.36; 'why': 0.37; 'quite': 0.37; 'subject:: ': 0.38; 'store': 0.38; 'files': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'think': 0.40; 'email addr:gmail.com': 0.63; 'more': 0.63; 'making': 0.64; 'carried': 0.65; 'jul': 0.65; 'header:Reply-To:1': 0.68; 'reply-to:no real name:2**0': 0.72; 'reply-to:addr:python.org': 0.84
X-CM-Score 0.00
X-CNFS-Analysis v=2.0 cv=FLuZNpUs c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=DKcI9XZsuF4A:10 a=mzP1dWKP1KAA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=pGLkceISAAAA:8 a=5KNB24frAAAA:8 a=6PjM4Mh_tpZ8kynIsocA:9 a=wPNLvfGTeEIA:10 a=MSl-tDqOz04A:10 a=6Ml3i-1dCzUA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117
X-AUTH mrabarnett:2500
Date Thu, 19 Jul 2012 22:32:46 +0100
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20120713 Thunderbird/14.0
MIME-Version 1.0
To python-list@python.org
Subject Re: Finding duplicate file names and modifying them based on elements of the path
References <b2f1993c-8872-44ed-9e69-0895e4059532@mi5g2000pbc.googlegroups.com> <mailman.2281.1342658195.4697.python-list@python.org> <aad5b32d-b0b4-47c1-b134-4827ec0472b2@t20g2000yqn.googlegroups.com> <mailman.2313.1342724574.4697.python-list@python.org> <3a201907-c6dd-4c1f-b921-6f508d0af6e8@r3g2000yqh.googlegroups.com>
In-Reply-To <3a201907-c6dd-4c1f-b921-6f508d0af6e8@r3g2000yqh.googlegroups.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
Reply-To python-list@python.org
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2319.1342733568.4697.python-list@python.org> (permalink)
Lines 75
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1342733568 news.xs4all.nl 6884 [2001:888:2000:d::a6]:59771
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:25653

Show key headers only | View raw


On 19/07/2012 20:06, Larry.Martell@gmail.com wrote:
> On Jul 19, 1:02 pm, "Prasad, Ramit" <ramit.pra...@jpmorgan.com> wrote:
>> > > I am making the assumption that you intend to collapse the directory
>> > > tree and store each file in the same directory, otherwise I can't think
>> > > of why you need to do this.
>>
>> > Hi Simon, thanks for the reply. It's not quite this - what I am doing
>> > is creating a zip file with relative path names, and if there are
>> > duplicate files the parts of the path that are not be carried over
>> > need to get prepended to the file names to make then unique,
>>
>> Depending on the file system of the client, you can hit file name
>> length limits. I would think it would be better to just create
>> the full structure in the zip.
>>
>> Just something to keep in mind, especially if you see funky behavior.
>
> Thanks, but it's not what the client wants.
>
Here's another solution, not using itertools:

from collections import defaultdict
from os.path import basename, dirname
from time import strftime, strptime

# Starting with the original paths

paths = [
     "/dir0/dir1/dir2/dir3/qwer/09Jan12/dir6/file3",
     "/dir0/dir1/dir2/dir3/abcd/08Jan12/dir6/file1",
     "/dir0/dir1/dir2/dir3/abcd/08Jan12/dir6/file2",
     "/dir0/dir1/dir2/dir3/xyz/08Jan12/dir6/file1",
     "/dir0/dir1/dir2/dir3/qwer/07Jan12/dir6/file3",
]

def make_dir5_key(path):
     date = strptime(path.split("/")[6], "%d%b%y")
     return strftime("%y%b%d", date)

# Collect the paths into a dict keyed by the basename

files = defaultdict(list)
for path in paths:
     files[basename(path)].append(path)

# Process a list of paths if there's more than one entry

renaming = []

for name, entries in files.items():
     if len(entries) > 1:
         # Collect the paths in each subgroup into a dict keyed by dir4

         subgroup = defaultdict(list)
         for path in entries:
             subgroup[path.split("/")[5]].append(path)

         for dir4, subentries in subgroup.items():
             # Sort the subentries by dir5 (date)
             subentries.sort(key=make_dir5_key)

             if len(subentries) > 1:
                 for index, path in enumerate(subentries):
                     renaming.append((path, 
"{}/{}_{:02}_{}".format(dirname(path), dir4, index, name)))
             else:
                 path = subentries[0]
                 renaming.append((path, "{}/{}_{}".format(dirname(path), 
dir4, name)))
     else:
         path = entries[0]

for old_path, new_path in renaming:
     print("Rename {!r} to {!r}".format(old_path, new_path))

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Finding duplicate file names and modifying them based on elements of the path "Larry.Martell@gmail.com" <larry.martell@gmail.com> - 2012-07-18 15:20 -0700
  Re: Finding duplicate file names and modifying them based on elements of the path Paul Rubin <no.email@nospam.invalid> - 2012-07-18 15:49 -0700
    Re: Finding duplicate file names and modifying them based on elements of the path "Larry.Martell@gmail.com" <larry.martell@gmail.com> - 2012-07-19 12:00 -0700
      Re: Finding duplicate file names and modifying them based on elements of the path Paul Rubin <no.email@nospam.invalid> - 2012-07-19 12:43 -0700
        Re: Finding duplicate file names and modifying them based on elements of the path "Larry.Martell@gmail.com" <larry.martell@gmail.com> - 2012-07-19 18:01 -0700
          Re: Finding duplicate file names and modifying them based on elements of the path Peter Otten <__peter__@web.de> - 2012-07-20 09:35 +0200
          Re: Finding duplicate file names and modifying them based on elements of the path Paul Rubin <no.email@nospam.invalid> - 2012-07-20 00:51 -0700
          Re: Finding duplicate file names and modifying them based on elements of the path Paul Rudin <paul.nospam@rudin.co.uk> - 2012-07-20 09:37 +0100
    Re: Finding duplicate file names and modifying them based on elements of the path "Larry.Martell@gmail.com" <larry.martell@gmail.com> - 2012-07-19 11:52 -0700
      Re: Finding duplicate file names and modifying them based on elements of the path Paul Rubin <no.email@nospam.invalid> - 2012-07-19 12:56 -0700
        Re: Finding duplicate file names and modifying them based on elements of the path "Larry.Martell@gmail.com" <larry.martell@gmail.com> - 2012-07-19 17:58 -0700
  Re: Finding duplicate file names and modifying them based on elements of the path Simon Cropper <simoncropper@fossworkflowguides.com> - 2012-07-19 10:36 +1000
    Re: Finding duplicate file names and modifying them based on elements of the path "Larry.Martell@gmail.com" <larry.martell@gmail.com> - 2012-07-19 11:54 -0700
      RE: Finding duplicate file names and modifying them based on elements of the path "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-07-19 19:02 +0000
        Re: Finding duplicate file names and modifying them based on elements of the path "Larry.Martell@gmail.com" <larry.martell@gmail.com> - 2012-07-19 12:06 -0700
          Re: Finding duplicate file names and modifying them based on elements of the path MRAB <python@mrabarnett.plus.com> - 2012-07-19 22:32 +0100
            Re: Finding duplicate file names and modifying them based on elements of the path "Larry.Martell@gmail.com" <larry.martell@gmail.com> - 2012-07-19 18:01 -0700
              Re: Finding duplicate file names and modifying them based on elements of the path "Larry.Martell@gmail.com" <larry.martell@gmail.com> - 2012-07-19 20:07 -0700
                Re: Finding duplicate file names and modifying them based on elements of the path MRAB <python@mrabarnett.plus.com> - 2012-07-20 16:45 +0100

csiph-web