The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Loop output listdir
Discuss Loop output listdir in the Python Programming forum on Dev Shed. Loop output listdir Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 8th, 2012, 12:14 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 3
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
|

November 8th, 2012, 02:59 PM
|
 |
Contributing User
|
|
|
|
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!
|

November 8th, 2012, 03:09 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 3
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()
|

November 8th, 2012, 03:41 PM
|
 |
Contributing User
|
|
|
|
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)
|

November 9th, 2012, 08:40 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 3
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.
|

November 9th, 2012, 10:03 AM
|
 |
Contributing User
|
|
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|