Reputation: 13
I try to find a string in a filename derrived from os.scandir() Like the string '15' in 'ilikecake15.pdf' or 'ilike1615cake.pdf' The strings are unique. Background for this code: I have several folders with pdfs that I need to match to a list. The filename contains parts of the strings from the list. I thought using to for loops would do the trick, one getting info from a txt. and the other from os.scandir() and find() to check for != -1.
Troubleshooting has left med with two .txt of values that I try to compare with s.find(ins). My goal is to sort out the != -1. It seems to work if I define v1 and v2 and do v1.find(v2) but in a nested for loop, everything comes out as -1. (even when I see the correct matchup) I have removed pathsinfo like G:\thispath\and\thispath from the textfile using [xx]. Even try adding str() to almost everything (just in case)
import os
tdpath = 'G:\Somepathwithalotofpdfs'
tdfiles = 'G:\Anothersuitablepath/tdfiles.txt'
tdlines = 'G:\Anothersuitablepath/tdlines.txt'
with os.scandir(tdpath) as pth, open(tdfiles, 'w') as fls:
for td in pth:
if td.is_file():
fls.write(str(os.fsdecode(td)[51:])) #str and [51:] added to help troubleshoot
fls.write('\n')
with open(tdlines) as fp, open(tdfiles) as fls:
for cnt, line in enumerate(fp):
for cmt, lajn in enumerate(fls):
print(lajn, line) #just troubleshooting
print(str(lajn).find(str(line))) #just troubleshooting
if lajn.find(line) != -1:
print('KASWSOFJIDFHAIFHSIHFIAHS') # Hlep ples
This has turned into a monster while trying to troubleshoot it. But the expected result is to print the latter statement when it finds a match from file tdlines.txt in tdfiles.txt
Upvotes: 1
Views: 312
Reputation: 7840
I'm not sure I exactly understand the nature of the problem you're describing, but I do see one thing that almost certainly doesn't behave as you expect.
Once you iterate over all the lines of an open file object, attempting to iterate over it again will yield nothing because the internal pointer keeping track of the working location in the file is now at the end. So for the first line of tdlines
, your loops will behave as you expect, but for the rest of the lines, the inner for
loop will never run.
The solution is either to close and reopen tdfiles
on each iteration through tdlines
, or seek()
back to the beginning of the file just before the inner for
loop. Here's the latter approach:
with open(tdlines) as fp, open(tdfiles) as fls:
for cnt, line in enumerate(fp):
fls.seek(0)
for cmt, lajn in enumerate(fls):
if lajn.find(line) != -1:
print('KASWSOFJIDFHAIFHSIHFIAHS')
Upvotes: 1