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 :( ')
python-3.x for-loop glob
add a comment |
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 :( ')
python-3.x for-loop glob
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 inmatches
. Just domatches = open(...).readlines()
or something. FYI if you want to get just the filename or directory parts of a path you can useos.path.dirname
andos.path.basename
. You can also split file extension withos.path.splitext
Your current code will break if a filenamecontains.multiple.dots
.
– Iguananaut
Nov 9 at 19:17
add a comment |
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 :( ')
python-3.x for-loop glob
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
python-3.x for-loop glob
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 inmatches
. Just domatches = open(...).readlines()
or something. FYI if you want to get just the filename or directory parts of a path you can useos.path.dirname
andos.path.basename
. You can also split file extension withos.path.splitext
Your current code will break if a filenamecontains.multiple.dots
.
– Iguananaut
Nov 9 at 19:17
add a comment |
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 inmatches
. Just domatches = open(...).readlines()
or something. FYI if you want to get just the filename or directory parts of a path you can useos.path.dirname
andos.path.basename
. You can also split file extension withos.path.splitext
Your current code will break if a filenamecontains.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
add a comment |
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))
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
add a comment |
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))
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
add a comment |
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))
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
add a comment |
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))
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))
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 domatches = open(...).readlines()
or something. FYI if you want to get just the filename or directory parts of a path you can useos.path.dirname
andos.path.basename
. You can also split file extension withos.path.splitext
Your current code will break if a filenamecontains.multiple.dots
.– Iguananaut
Nov 9 at 19:17