#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Loop output listdir


    First time using Python and what I need to do if produce and output file from my downloads directory. but I think I need a dictionary or something in order to not output the same file name multiple times.

    this is the script:
    Code:
    CSN = ['12275632444', '12275627859',  '12275627856']         
    for CSNumber in CSN:             driver.find_element_by_id("fnsearchuc_temp2__ctl14_tbSimple").clear()             driver.find_element_by_id("fnsearchuc_temp2__ctl14_tbSimple").send_keys(CSNumber)             driver.find_element_by_css_selector("span[title=\"Find Now\"]").click()             
    time.sleep(1)             
    # need to check if no img open then go to next claim number             try:                 driver.find_element_by_css_selector("img[alt=\"Open:\"]").click()             
    except:                 
    outputCSN = open('outputCSN.txt', 'a')                 
    outputCSN.write('There was an error with CSN:'+CSNumber)                 
    outputCSN.close()             
    time.sleep(2)             
    #x and y axis is based on the position of the screen scrap browser not the save dialog box              
    self.save_click(570,540)             time.sleep(2)              
    # create output file             
    for fileName in os.listdir('N:\MyDocuments\Downloads'):                 
    outputCSN = open('outputCSN.txt', 'a')                 
    # adict={fileName:CSNumber}                 
    outputStrVal = str(CSNumber + ',0,' + 'N:\\My Documents\\Downloads\\' + fileName + '\n')                 outputCSN.write(outputStrVal)                 
    outputCSN.close()
    my output file looks like this:
    12275632444,0,N:\My Documents\Downloads\201227510028031.tif
    12275627859,0,N:\My Documents\Downloads\201227510024805.tif
    12275627859,0,N:\My Documents\Downloads\201227510028031.tif
    12275627856,0,N:\My Documents\Downloads\201227510024802.tif
    12275627856,0,N:\My Documents\Downloads\201227510024805.tif
    12275627856,0,N:\My Documents\Downloads\201227510028031.tif

    I want it to be like this:
    12275632444,0,N:\My Documents\Downloads\201227510028031.tif
    12275627859,0,N:\My Documents\Downloads\201227510024805.tif
    12275627856,0,N:\My Documents\Downloads\201227510024802.tif
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,894
    Rep Power
    481
    As you know, leading white space in python is an important syntactic element. I can't really tell how your program should be since you posted it as a blob. Please follow the link, and read the information you find, to improve your next post.

    Yes, a dictionary keyed by the filename would help.
    Code:
    d = dict()
    for whatevers:
        d[filename] = CSNumber
    Also, open and close the file outside the loop.
    Code:
    with open(outputFile,'a') as ouf:
        for Object in Objects:
            ouf.write(whatever)
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0
    Thanks, Here is a better look at the code:

    Code:
            CSN = ['12275632444', '12275627859',  '12275627856']
                  
            for CSNumber in CSN:
                driver.find_element_by_id("fnsearchuc_temp2__ctl14_tbSimple").clear()
                driver.find_element_by_id("fnsearchuc_temp2__ctl14_tbSimple").send_keys(CSNumber)
                driver.find_element_by_css_selector("span[title=\"Find Now\"]").click()
                time.sleep(1)
                # need to check if no img open then go to next claim number
                driver.find_element_by_css_selector("img[alt=\"Open:\"]").click()
                # try:
                #    driver.find_element_by_css_selector("img[alt=\"Open:\"]").click()
                #except:
                #    outputCSN = open('outputCSN.txt', 'a')
                #    outputCSN.write('There was an error with CSN:'+CSNumber)
                #    outputCSN.close()
                time.sleep(2)
                # x and y axis is based on the position of the screen scrap browser not the save dialog box 
                self.save_click(570,540)
                time.sleep(2)
    
                # create output file
                for fileName in os.listdir('N:\My Documents\Downloads'):
                    outputCSN = open('outputCSN.txt', 'a')
                    # my_dict={fileName:CSNumber}
                    outputStrVal = str(CSNumber + ',0,' + 'N:\\My Documents\\Downloads\\' + fileName + '\n')
                    outputCSN.write(outputStrVal)
                    outputCSN.close()
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,894
    Rep Power
    481
    Sets might help more directly. (Note that sets were first implemented with dictionaries, and that's why my answer "dictionaries could help" was fairly safe.)

    Code:
    CSN = ['12275632444', '12275627859',  '12275627856']
    
    written = set()
    
    for CSNumber in CSN:
        driver.find_element_by_id("fnsearchuc_temp2__ctl14_tbSimple").clear()
        driver.find_element_by_id("fnsearchuc_temp2__ctl14_tbSimple").send_keys(CSNumber)
        driver.find_element_by_css_selector("span[title=\"Find Now\"]").click()
        time.sleep(1)
        # need to check if no img open then go to next claim number
        driver.find_element_by_css_selector("img[alt=\"Open:\"]").click()
        # try:
        #    driver.find_element_by_css_selector("img[alt=\"Open:\"]").click()
        #except:
        #    outputCSN = open('outputCSN.txt', 'a')
        #    outputCSN.write('There was an error with CSN:'+CSNumber)
        #    outputCSN.close()
        time.sleep(2)
        # x and y axis is based on the position of the screen scrap browser not the save dialog box
        self.save_click(570,540)
        time.sleep(2)
    
        # create output file
        for fileName in os.listdir('N:\My Documents\Downloads'):
            if fileName not in written:
                outputCSN = open('outputCSN.txt', 'a')
                # my_dict={fileName:CSNumber}
                outputStrVal = str(CSNumber + ',0,' + 'N:\\My Documents\\Downloads\\' + fileName + '\n')
                outputCSN.write(outputStrVal)
                outputCSN.close()
                written.add(fileName)
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0
    Thanks that works nicely.

    I un-commented the try except to check to see if there is no image on the screen with the alt = to open. I used Selenium as a screen scraper and that is why the code looks the way it does. Anyway, my output also catches the thumbs.db file in the results:

    12275632444,0,N:\My Documents\Downloads\201227510028031.tif
    12275632444,0,N:\My Documents\Downloads\Thumbs.db
    12275627859,0,N:\My Documents\Downloads\201227510024805.tif
    12275627856,0,N:\My Documents\Downloads\201227510024802.tif

    I tried to fix this by showing all files in the download directory to see if I could see the thumbs.db file but they do not show. I think I'll try to save the files to a different location on the c: drive to see if that helps if not then add an if statement to the output file to look for the thumbs.db and if it exists remove it.

    Any thoughts?

    Again thanks for help, I needed it.
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,894
    Rep Power
    481
    Code:
    if not fileName.endswith('.tif'):
        continue
    I'm lost and confused. Still, if you put this sort of code in the right place in the right loop you'd process only the .tif files.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo