umgcoder
umgcoder

Reputation: 101

Verify part of file name exists in a dictionary or list, Python 2.7

I'm new to coding and working on the early stages of a file checking program. I'm writing a program to see whether a file contains "correct information" in its title or not.

I'm currently stuck on how to check to see if part of a title matches an acceptable name in a dictionary or list.

My code is below. If I put in a title that contains less than 3 parts, an error is thrown. However, if I put in a title with 3 parts, even if none of the parts match anything in the lists or dictionary, my program claims the title is correct (which it's not).

I know the if-statements are rough because of all the or's, but it's the best idea I have right now in lieu of writing a ton of if-statements for each part.

Can anybody help me with correcting the code so that I can check part of a title against a list or dictionary to ensure that part exists in the list/dictionary?

An example (correct) file name would be: DJ_BR_UVT.xls and an example of an incorrect file name is:DJ_BR_staford.xls *as a side note, the parts or species, school, initials, can be in any order in the file name.

def checkFilename(filename):
    print 'filename = ' + filename
    Parts = filename.split('_')
if len(Parts) != 3:
    print "There are " + str(len(Parts)) + " that is an incorrect amount of info in file name. There should be 3 parts to your file name"
    return

Part1 = Parts[0]
Part2 = Parts[1]
Part3 = Parts[2]
Species_Dictionary = {'Brown Rat':'BR', 'Black Rat':'BLR', 'Dwarf Rat':'DR', 'White Mouse':'GG', 'Human':'HS', 'Brown Mouse':'VK'}
School_List = ['UHJ', 'UMG', 'COL', 'UVT']
Initials_List = ['DM', 'DCM', 'YXAA', 'DJ']
Species_Check = 0
School_Check = 0
Initials_Check = 0
# supposed to check to see if each 'part' can be found in the Species_Dictionary
if Part1 or Part2 or Part3 in Species_Dictionary:
    Species_Check = 1
    print Species_Check
else:
    print "Cannot find a valid species"
    return

#check to see if any of the 'parts' can be found in the School-List
if Part1 or Part2 or Part3 in School_List:
    School_Check = 1
else:
    print "Cannot find valid school"
    return

#Check if any of the 'parts' are in the Initials_List
if Part1 or Part2 or Part3 in Initials_List:
    Initials_Check = 1
else:
    print "Cannot find valid initials"
    return

#If the previous 3 if-statements have been met, the file 'passes' and contains correct info
if Species_Check == 1 and School_Check == 1 and Initials_Check == 1:
    print "Your file contains correct title information"
else:
    print "Your file name does not contain the correct information"
    return

Upvotes: 0

Views: 188

Answers (1)

Sci Prog
Sci Prog

Reputation: 2691

The condition if Part1 or Part2 or Part3 in Species_Dictionary: will not do what you think.

If the file name is DJ_BR_UVT.xls then the parts will be DJ, BR and UVT.xls. You have to remove the extension.

PARTS1 = ('BR','BLR','DR','GG','HS','VK')
PARTS2 = ('UHJ', 'UMG', 'COL', 'UVT')
PARTS3 = ('DM', 'DCM', 'YXAA', 'DJ')
def checkFilename(filename):
  f = filename.split('.')[0]   # this removes the extension
  parts = f.split('_')
  nb1, nb2, nb3 = 0, 0, 0
  for p in parts:
    if p in PARTS1: nb1 += 1
    if p in PARTS2: nb2 += 1
    if p in PARTS3: nb3 += 1
  return nb1 == 1 and nb2 == 1 and nb3 == 1

print (checkFilename("DJ_BR_UVT.xls"))
print (checkFilename("DJ_BR_staford.xls"))

this prints

True
False

Upvotes: 1

Related Questions