Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 8th, 2012, 12:14 PM
rsvore rsvore is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 3 rsvore User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 25 m 17 sec
Reputation 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

Reply With Quote
  #2  
Old November 8th, 2012, 02:59 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is online now
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,458 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 33 m 33 sec
Reputation Power: 403
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!

Reply With Quote
  #3  
Old November 8th, 2012, 03:09 PM
rsvore rsvore is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 3 rsvore User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 25 m 17 sec
Reputation 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()

Reply With Quote
  #4  
Old November 8th, 2012, 03:41 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is online now
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,458 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 33 m 33 sec
Reputation Power: 403
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)

Reply With Quote
  #5  
Old November 9th, 2012, 08:40 AM
rsvore rsvore is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 3 rsvore User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 25 m 17 sec
Reputation 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.

Reply With Quote
  #6  
Old November 9th, 2012, 10:03 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is online now
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,458 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 33 m 33 sec
Reputation Power: 403
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Loop output listdir

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap