question

Discussion in 'Python' started by JYOUNG79@kc.rr.com, Aug 24, 2007.

  1. Guest

    Still trying to learn Python and was hoping some of you might be able to give me some advice
    on my code
    below. It works but I'm wondering if there's more efficient ways of doing this than the way
    I've done it.

    The first step is I have to loop thru nested folders and find all files that start with "LOGO". So
    I use Unix's 'find'
    and write all the paths out to a text file like so:

    find ~/Desktop/logoFiles/ -type f -iname "LOGO*" > ~/Desktop/logo_stuff/paths.txt"

    Next, I use Python (code below) to look thru each paragraph of that file (paths.txt). While
    looping thru each
    paragraph, I want to first make sure that the folder name that the file is in is a number. For
    example, if it found
    file 'LOGO9012', then I'd want to make sure the folder name that this file was in was a
    number (~/Desktop/
    logoFiles/12/LOGO9012). If that's ok then I grab the number off the file name. If there's not
    a number in the
    file name then I ignore it. I then create a dictionary that has a key which equals the number I
    grabbed from the
    file name and then the value contains the full file path (that I grabbed out of the text file)
    followed by a
    delimiter (<::>) followed by 'LOGO' and the number I got from the file name (making sure to
    keep any leading
    zeroes if they were there originally). Finally, I sort the keys in numerical order and save out a
    file with all the
    appropriate info from my dictionary (which will be a list of paths followed by a delimiter and
    file name). Here's
    an example of what this new file might contain:

    /Users/jyoung1/Desktop/logoFiles/02/Logo002<::>LOGO002
    /Users/jyoung1/Desktop/logoFiles/02/LOGO102<::>LOGO102
    /Users/jyoung1/Desktop/logoFiles/02/LOGO302<::>LOGO302
    /Users/jyoung1/Desktop/logoFiles/9/LOGO462.2PMS<::>LOGO462


    Anyway, if anyone has time to look at this I'd appreciate your thoughts. Thanks!

    Jay

    #!/usr/bin/python

    import re

    dList = {}
    sortedList = ""

    pathFile = open("~/Desktop/logo_stuff/paths.txt", "r")

    for x in pathFile:
    if len(re.findall(r"^\d+$", x.split("/")[-2])) > 0: #make sure folder name is a number
    n = re.findall(r"\d+", x.split("/")[-1]) #Grab number off file name
    if len(n) > 0: dList[int(n[0])] = x[:-1] + "<::>LOGO" + n[0] + "\n"

    pathFile.close()

    keyList = dList.keys()
    keyList.sort()

    for x in keyList:
    sortedList += dList[x]

    newFile = open("~/Desktop/logo_stuff/sortedPaths.txt", "w")
    newFile.write(sortedList)
    newFile.close()
     
    , Aug 24, 2007
    #1
    1. Advertising

  2. MRAB Guest

    On Aug 24, 7:20 pm, wrote:
    > Still trying to learn Python and was hoping some of you might be able to give me some advice
    > on my code
    > below. It works but I'm wondering if there's more efficient ways of doing this than the way
    > I've done it.
    >
    > The first step is I have to loop thru nested folders and find all files that start with "LOGO". So
    > I use Unix's 'find'
    > and write all the paths out to a text file like so:
    >
    > find ~/Desktop/logoFiles/ -type f -iname "LOGO*" > ~/Desktop/logo_stuff/paths.txt"
    >
    > Next, I use Python (code below) to look thru each paragraph of that file (paths.txt). While
    > looping thru each
    > paragraph, I want to first make sure that the folder name that the file is in is a number. For
    > example, if it found
    > file 'LOGO9012', then I'd want to make sure the folder name that this file was in was a
    > number (~/Desktop/
    > logoFiles/12/LOGO9012). If that's ok then I grab the number off the file name. If there's not
    > a number in the
    > file name then I ignore it. I then create a dictionary that has a key which equals the number I
    > grabbed from the
    > file name and then the value contains the full file path (that I grabbed out of the text file)
    > followed by a
    > delimiter (<::>) followed by 'LOGO' and the number I got from the file name (making sure to
    > keep any leading
    > zeroes if they were there originally). Finally, I sort the keys in numerical order and save out a
    > file with all the
    > appropriate info from my dictionary (which will be a list of paths followed by a delimiter and
    > file name). Here's
    > an example of what this new file might contain:
    >
    > /Users/jyoung1/Desktop/logoFiles/02/Logo002<::>LOGO002
    > /Users/jyoung1/Desktop/logoFiles/02/LOGO102<::>LOGO102
    > /Users/jyoung1/Desktop/logoFiles/02/LOGO302<::>LOGO302
    > /Users/jyoung1/Desktop/logoFiles/9/LOGO462.2PMS<::>LOGO462
    >
    > Anyway, if anyone has time to look at this I'd appreciate your thoughts. Thanks!
    >
    > Jay
    >
    > #!/usr/bin/python
    >
    > import re
    >
    > dList = {}
    > sortedList = ""
    >
    > pathFile = open("~/Desktop/logo_stuff/paths.txt", "r")
    >
    > for x in pathFile:
    > if len(re.findall(r"^\d+$", x.split("/")[-2])) > 0: #make sure folder name is a number
    > n = re.findall(r"\d+", x.split("/")[-1]) #Grab number off file name
    > if len(n) > 0: dList[int(n[0])] = x[:-1] + "<::>LOGO" + n[0] + "\n"
    >
    > pathFile.close()
    >
    > keyList = dList.keys()
    > keyList.sort()
    >
    > for x in keyList:
    > sortedList += dList[x]
    >
    > newFile = open("~/Desktop/logo_stuff/sortedPaths.txt", "w")
    > newFile.write(sortedList)
    > newFile.close()
    >

    Here's my attempt:

    #!/usr/bin/python

    import os
    import re

    # Search for the files

    folder = "~/Desktop/logoFiles/"

    pathList = []

    for name in os.listdir(folder):
    path = os.path.join(folder, name)
    # If you want it to be case-insensitive then use
    name.upper().startswith("LOGO")
    if os.path.isfile(path) and name.startswith("LOGO"):
    pathList.append(path)

    dList = {}

    for x in pathList:
    parts = x.split("/")
    if parts[-2].isdigit(): # Make sure folder name is a number
    n = re.findall(r"(\d+)$", parts[-1]) # Grab number off file
    name
    if n:
    dList[int(n[0])] = "%s<::>LOGO%s\n" % (x, n[0])

    keyList = dList.keys()
    keyList.sort()

    sortedList = "".join(dList[x] for x in keyList)

    newFile = open("~/Desktop/logo_stuff/sortedPaths.txt", "w")
    newFile.write(sortedList)
    newFile.close()
     
    MRAB, Aug 25, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sean
    Replies:
    1
    Views:
    613
    Cowboy \(Gregory A. Beamer\)
    Oct 20, 2003
  2. =?Utf-8?B?UnlhbiBTbWl0aA==?=

    Quick Question - Newby Question

    =?Utf-8?B?UnlhbiBTbWl0aA==?=, Feb 14, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    682
    Iain Norman
    Feb 16, 2005
  3. =?Utf-8?B?YW5kcmV3MDA3?=

    question row filter (more of sql query question)

    =?Utf-8?B?YW5kcmV3MDA3?=, Oct 5, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    963
    Scott Allen
    Oct 6, 2005
  4. Philip Meyer
    Replies:
    0
    Views:
    438
    Philip Meyer
    Nov 30, 2003
  5. Bit Byte
    Replies:
    1
    Views:
    870
    Teemu Keiski
    Jan 28, 2007
Loading...

Share This Page