Skip to content
Advertisement

How to check the windows path matches with partial Linux path string

I am trying to check what files that are present in my full_list_files are also present in required_list.
The thing here is they are not exactly equal to one other , but macthes with filename and last sub directory.

Example :
'C:UsersDocumentsUpdatedBuildOutputMApplication_1.bin' matches with "M/Application_1.bin" except the slashes are different.
So I am trying to make both uniform by using the function convert_fslash_2_bslash

But still, I see the output as below ,none of the files are matched.

full_list_files =  set(['C:\Users\Documents\Updated\Build\Output\O\Report.tar.gz', 'C:\Users\Documents\Updated\Build\Output\N\Application_2.bin', 'C:\Users\Documents\Updated\Build\Output\O\Testing.txt', 'C:\Users\Documents\Updated\Build\Output\M\masking.tar.gz', 'C:\Users\Documents\Updated\Build\Output\N\Application_1.bin', 'C:\Users\Documents\Updated\Build\Output\M\Application_1.bin', 'C:\Users\Documents\Updated\Build\Output\O\History.zip', 'C:\Users\Documents\Updated\Build\Output\O\Challenge.tar.gz', 'C:\Users\Documents\Updated\Build\Output\M\Application_2.bin', 'C:\Users\Documents\Updated\Build\Output\N\porting.tar.gz', 'C:\Users\Documents\Updated\Build\Output\M\Booting.tar.gz'])

original required_list =  set(['N/Application_2.bin', 'M/masking.tar.gz', 'N/Application_1.bin', 'O/Challenge.tar.gz', 'M/Application_1.bin', 'O/Testing.txt', 'M/rooting.tar.gz', 'M/Application_2.bin', 'O/History.zip', 'N/porting.tar.gz', 'O/Report.tar.gz'])

modified required_list =  ['N\Application_2.bin', 'M\masking.tar.gz', 'N\Application_1.bin', 'O\Challenge.tar.gz', 'M\Application_1.bin', 'O\Testing.txt', 'M\rooting.tar.gz', 'M\Application_2.bin', 'O\History.zip', 'N\porting.tar.gz', 'O\Report.tar.gz']

'C:\Users\Documents\Updated\Build\Output\O\Report.tar.gz'  not present
'C:\Users\Documents\Updated\Build\Output\N\Application_2.bin'  not present
'C:\Users\Documents\Updated\Build\Output\O\Testing.txt'  not present
'C:\Users\Documents\Updated\Build\Output\M\masking.tar.gz'  not present
'C:\Users\Documents\Updated\Build\Output\N\Application_1.bin'  not present
'C:\Users\Documents\Updated\Build\Output\M\Application_1.bin'  not present
'C:\Users\Documents\Updated\Build\Output\O\History.zip'  not present
'C:\Users\Documents\Updated\Build\Output\O\Challenge.tar.gz'  not present
'C:\Users\Documents\Updated\Build\Output\M\Application_2.bin'  not present
'C:\Users\Documents\Updated\Build\Output\N\porting.tar.gz'  not present
'C:\Users\Documents\Updated\Build\Output\M\Booting.tar.gz'  not present

How can I get it working correctly.

import os
import sys
import re

full_list_files = {
    #These are actually real paths parsed from listdir
    #Just for convenience used as strings
    'C:UsersDocumentsUpdatedBuildOutputMApplication_1.bin',
    'C:UsersDocumentsUpdatedBuildOutputMApplication_2.bin',
    'C:UsersDocumentsUpdatedBuildOutputMmasking.tar.gz',
    'C:UsersDocumentsUpdatedBuildOutputMBooting.tar.gz',
    'C:UsersDocumentsUpdatedBuildOutputNApplication_1.bin',
    'C:UsersDocumentsUpdatedBuildOutputNApplication_2.bin',
    'C:UsersDocumentsUpdatedBuildOutputNporting.tar.gz',
    'C:UsersDocumentsUpdatedBuildOutputOChallenge.tar.gz',
    'C:UsersDocumentsUpdatedBuildOutputOHistory.zip',
    'C:UsersDocumentsUpdatedBuildOutputOTesting.txt',
    'C:UsersDocumentsUpdatedBuildOutputOReport.tar.gz'
}

required_list = {
    "M/Application_1.bin",
    "M/Application_2.bin",
    "M/masking.tar.gz",
    "M/rooting.tar.gz",
    "N/Application_1.bin",
    "N/Application_2.bin",
    "N/porting.tar.gz",
    "O/Challenge.tar.gz",
    "O/History.zip",
    "O/Testing.txt",
    "O/Report.tar.gz"
}

def convert_fslash_2_bslash(required_file_list):
    required_config_file_list = []
    i = 0
    for entry in required_file_list:
        entry = entry.strip()
        entry = entry.replace('"',"")
        entry = entry.replace('/','\')
        required_config_file_list.insert(i, entry)
        i = i + 1
    return required_config_file_list

if __name__ == "__main__":

    print

    print "full_list_files = ", full_list_files

    print

    print "original required_list = ", required_list

    print

    required_config_file_list = convert_fslash_2_bslash(required_list)

    print "modified required_list = ", required_config_file_list

    print

    for f_entry in full_list_files:
        f_entry = repr(f_entry)
        #for r_entry in required_config_file_list:
        #if ( f_entry.find(r_entry) != -1):
        if f_entry in required_config_file_list:
            print f_entry ," present"
        else:
            print f_entry ," not present"

Advertisement

Answer

Here is the logic you need at the bottom:

for f_entry in full_list_files:
    for r_entry in required_config_file_list:
        if f_entry.endswith(r_entry):
            print f_entry, " present"

You need to loop over both collections, then check to see if the longer path ends with the shorter path. One of your mistakes was calling repr(), which changes the double backslashes to quadruple ones.

I’ll leave it up to you to decide how you’ll handle printing paths that are not present at all.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement