Re-loop until all matches are found, logic?









up vote
0
down vote

favorite












I cannot figure out the logic for this. I am attempting to compare a list of matches 'matches' to files from a folder. If file in 'folders' equal the name in 'matches', then do something, but obviously it doesn't 'try' each match to each file. I'm thinking I need to use a while loop but I don't know how to apply it.



import os
import glob
import os.path

folders = glob.glob('C:\Corrections\*.*')
matches = open('filename.txt', 'r')

for each in folders:
splitname_one = each.split('\', 3) #Separate the filename from the path
filename = splitname_one[3] #Get Filename only
basefile = filename.split('.', 1) #Separate filename and file extension
compare0 = basefile[0] #assign base file name to compare0
#print (basefile[0])
for line in matches:
match = line.split('.', 1) #Separe base filename from file extension
#print (match[1])
compare1 = match[0] #assign base file name to compare1
if compare1==compare0:
#os.rename(filename, 'C:\holder\' + filename)
print ('We Have a match!')
else:
print ('no match :( ')









share|improve this question





















  • Your outer loop loops over the file object each time. When the file reaches EOF the first time you loop through it, subsequent passes through your outer loop won't do anything because there are no lines left in matches. Just do matches = open(...).readlines() or something. FYI if you want to get just the filename or directory parts of a path you can use os.path.dirname and os.path.basename. You can also split file extension with os.path.splitext Your current code will break if a filename contains.multiple.dots.
    – Iguananaut
    Nov 9 at 19:17















up vote
0
down vote

favorite












I cannot figure out the logic for this. I am attempting to compare a list of matches 'matches' to files from a folder. If file in 'folders' equal the name in 'matches', then do something, but obviously it doesn't 'try' each match to each file. I'm thinking I need to use a while loop but I don't know how to apply it.



import os
import glob
import os.path

folders = glob.glob('C:\Corrections\*.*')
matches = open('filename.txt', 'r')

for each in folders:
splitname_one = each.split('\', 3) #Separate the filename from the path
filename = splitname_one[3] #Get Filename only
basefile = filename.split('.', 1) #Separate filename and file extension
compare0 = basefile[0] #assign base file name to compare0
#print (basefile[0])
for line in matches:
match = line.split('.', 1) #Separe base filename from file extension
#print (match[1])
compare1 = match[0] #assign base file name to compare1
if compare1==compare0:
#os.rename(filename, 'C:\holder\' + filename)
print ('We Have a match!')
else:
print ('no match :( ')









share|improve this question





















  • Your outer loop loops over the file object each time. When the file reaches EOF the first time you loop through it, subsequent passes through your outer loop won't do anything because there are no lines left in matches. Just do matches = open(...).readlines() or something. FYI if you want to get just the filename or directory parts of a path you can use os.path.dirname and os.path.basename. You can also split file extension with os.path.splitext Your current code will break if a filename contains.multiple.dots.
    – Iguananaut
    Nov 9 at 19:17













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I cannot figure out the logic for this. I am attempting to compare a list of matches 'matches' to files from a folder. If file in 'folders' equal the name in 'matches', then do something, but obviously it doesn't 'try' each match to each file. I'm thinking I need to use a while loop but I don't know how to apply it.



import os
import glob
import os.path

folders = glob.glob('C:\Corrections\*.*')
matches = open('filename.txt', 'r')

for each in folders:
splitname_one = each.split('\', 3) #Separate the filename from the path
filename = splitname_one[3] #Get Filename only
basefile = filename.split('.', 1) #Separate filename and file extension
compare0 = basefile[0] #assign base file name to compare0
#print (basefile[0])
for line in matches:
match = line.split('.', 1) #Separe base filename from file extension
#print (match[1])
compare1 = match[0] #assign base file name to compare1
if compare1==compare0:
#os.rename(filename, 'C:\holder\' + filename)
print ('We Have a match!')
else:
print ('no match :( ')









share|improve this question













I cannot figure out the logic for this. I am attempting to compare a list of matches 'matches' to files from a folder. If file in 'folders' equal the name in 'matches', then do something, but obviously it doesn't 'try' each match to each file. I'm thinking I need to use a while loop but I don't know how to apply it.



import os
import glob
import os.path

folders = glob.glob('C:\Corrections\*.*')
matches = open('filename.txt', 'r')

for each in folders:
splitname_one = each.split('\', 3) #Separate the filename from the path
filename = splitname_one[3] #Get Filename only
basefile = filename.split('.', 1) #Separate filename and file extension
compare0 = basefile[0] #assign base file name to compare0
#print (basefile[0])
for line in matches:
match = line.split('.', 1) #Separe base filename from file extension
#print (match[1])
compare1 = match[0] #assign base file name to compare1
if compare1==compare0:
#os.rename(filename, 'C:\holder\' + filename)
print ('We Have a match!')
else:
print ('no match :( ')






python-3.x for-loop glob






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 18:24









MPineda

19029




19029











  • Your outer loop loops over the file object each time. When the file reaches EOF the first time you loop through it, subsequent passes through your outer loop won't do anything because there are no lines left in matches. Just do matches = open(...).readlines() or something. FYI if you want to get just the filename or directory parts of a path you can use os.path.dirname and os.path.basename. You can also split file extension with os.path.splitext Your current code will break if a filename contains.multiple.dots.
    – Iguananaut
    Nov 9 at 19:17

















  • Your outer loop loops over the file object each time. When the file reaches EOF the first time you loop through it, subsequent passes through your outer loop won't do anything because there are no lines left in matches. Just do matches = open(...).readlines() or something. FYI if you want to get just the filename or directory parts of a path you can use os.path.dirname and os.path.basename. You can also split file extension with os.path.splitext Your current code will break if a filename contains.multiple.dots.
    – Iguananaut
    Nov 9 at 19:17
















Your outer loop loops over the file object each time. When the file reaches EOF the first time you loop through it, subsequent passes through your outer loop won't do anything because there are no lines left in matches. Just do matches = open(...).readlines() or something. FYI if you want to get just the filename or directory parts of a path you can use os.path.dirname and os.path.basename. You can also split file extension with os.path.splitext Your current code will break if a filename contains.multiple.dots.
– Iguananaut
Nov 9 at 19:17





Your outer loop loops over the file object each time. When the file reaches EOF the first time you loop through it, subsequent passes through your outer loop won't do anything because there are no lines left in matches. Just do matches = open(...).readlines() or something. FYI if you want to get just the filename or directory parts of a path you can use os.path.dirname and os.path.basename. You can also split file extension with os.path.splitext Your current code will break if a filename contains.multiple.dots.
– Iguananaut
Nov 9 at 19:17













1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










FWIW here's how I might end up doing something like this:



import glob
from os.path import basename, splitext

def file_base(filename):
return splitext(basename(filename))[0]

folders = set(file_base(f) for f in glob.glob('C:\Corrections\*.*'))

with open('filename.txt') as fobj:
matches = set(file_base(f) for f in fobj.readlines())

print(folders.intersection(matches))





share|improve this answer




















  • I will review this and comment back, I like to understand what each part of the code does even if it works :) Thank you.
    – MPineda
    Nov 9 at 19:38










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53231389%2fre-loop-until-all-matches-are-found-logic%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










FWIW here's how I might end up doing something like this:



import glob
from os.path import basename, splitext

def file_base(filename):
return splitext(basename(filename))[0]

folders = set(file_base(f) for f in glob.glob('C:\Corrections\*.*'))

with open('filename.txt') as fobj:
matches = set(file_base(f) for f in fobj.readlines())

print(folders.intersection(matches))





share|improve this answer




















  • I will review this and comment back, I like to understand what each part of the code does even if it works :) Thank you.
    – MPineda
    Nov 9 at 19:38














up vote
1
down vote



accepted










FWIW here's how I might end up doing something like this:



import glob
from os.path import basename, splitext

def file_base(filename):
return splitext(basename(filename))[0]

folders = set(file_base(f) for f in glob.glob('C:\Corrections\*.*'))

with open('filename.txt') as fobj:
matches = set(file_base(f) for f in fobj.readlines())

print(folders.intersection(matches))





share|improve this answer




















  • I will review this and comment back, I like to understand what each part of the code does even if it works :) Thank you.
    – MPineda
    Nov 9 at 19:38












up vote
1
down vote



accepted







up vote
1
down vote



accepted






FWIW here's how I might end up doing something like this:



import glob
from os.path import basename, splitext

def file_base(filename):
return splitext(basename(filename))[0]

folders = set(file_base(f) for f in glob.glob('C:\Corrections\*.*'))

with open('filename.txt') as fobj:
matches = set(file_base(f) for f in fobj.readlines())

print(folders.intersection(matches))





share|improve this answer












FWIW here's how I might end up doing something like this:



import glob
from os.path import basename, splitext

def file_base(filename):
return splitext(basename(filename))[0]

folders = set(file_base(f) for f in glob.glob('C:\Corrections\*.*'))

with open('filename.txt') as fobj:
matches = set(file_base(f) for f in fobj.readlines())

print(folders.intersection(matches))






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 9 at 19:25









Iguananaut

8,2452640




8,2452640











  • I will review this and comment back, I like to understand what each part of the code does even if it works :) Thank you.
    – MPineda
    Nov 9 at 19:38
















  • I will review this and comment back, I like to understand what each part of the code does even if it works :) Thank you.
    – MPineda
    Nov 9 at 19:38















I will review this and comment back, I like to understand what each part of the code does even if it works :) Thank you.
– MPineda
Nov 9 at 19:38




I will review this and comment back, I like to understand what each part of the code does even if it works :) Thank you.
– MPineda
Nov 9 at 19:38

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53231389%2fre-loop-until-all-matches-are-found-logic%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20