Skip to main content
Minor spelling/punctuation fixes
Source Link

Python function - Toto match filenames with extension names

I have written a pyhthonPython function, which matches all files in the current directory with a list of extension names.It It is working correctly.

import os, sys, time, re, stat

def matchextname(extnames, filename):
    # Need to build the regular expression from the list
    myregstring = ""
    for index in range(len(extnames)):
        # r1 union r2 and  so on operator is pipe(|)
        # $ is to match from the end
        if index <  len(extnames) - 1:
            myregstring =  myregstring + extnames[index] + '$' + '|'
        else:
            myregstring = myregstring + extnames[index] + '$'
    # getting regexobject
    myregexobj = re.compile(myregstring)
    # Now search
    searchstat = myregexobj.search(filename)
    if searchstat:
        print 'Regex', filename

It is called like this:

if __name__ == '__main__':
    fileextensions = ['\.doc', '\.o', '\.out', '\.c', '\.h']
    try:
        currentdir = os.getcwd()
    except OSError:
        print 'Error occured while getting current directory'
        sys.exit(1)
    for myfiles in os.listdir(currentdir):
        matchextname(fileextensions, myfiles)

Could you please review the code and suggest if there is any better way of doing this, or could you please share any other comments related to errorserrors/exception handlingshandling which are missing - or anything else in terms of logic?

Python function - To match filenames with extension names

I have written a pyhthon function, which matches all files in the current directory with a list of extension names.It is working correctly.

import os, sys, time, re, stat

def matchextname(extnames, filename):
    # Need to build the regular expression from the list
    myregstring = ""
    for index in range(len(extnames)):
        # r1 union r2 and  so on operator is pipe(|)
        # $ is to match from the end
        if index <  len(extnames) - 1:
            myregstring =  myregstring + extnames[index] + '$' + '|'
        else:
            myregstring = myregstring + extnames[index] + '$'
    # getting regexobject
    myregexobj = re.compile(myregstring)
    # Now search
    searchstat = myregexobj.search(filename)
    if searchstat:
        print 'Regex', filename

It is called like this:

if __name__ == '__main__':
    fileextensions = ['\.doc', '\.o', '\.out', '\.c', '\.h']
    try:
        currentdir = os.getcwd()
    except OSError:
        print 'Error occured while getting current directory'
        sys.exit(1)
    for myfiles in os.listdir(currentdir):
        matchextname(fileextensions, myfiles)

Could you please review the code and suggest if there is any better way of doing this or could you please share any other comments related to errors/exception handlings which are missing or anything else in terms of logic?

Python function to match filenames with extension names

I have written a Python function which matches all files in the current directory with a list of extension names. It is working correctly.

import os, sys, time, re, stat

def matchextname(extnames, filename):
    # Need to build the regular expression from the list
    myregstring = ""
    for index in range(len(extnames)):
        # r1 union r2 and  so on operator is pipe(|)
        # $ is to match from the end
        if index <  len(extnames) - 1:
            myregstring =  myregstring + extnames[index] + '$' + '|'
        else:
            myregstring = myregstring + extnames[index] + '$'
    # getting regexobject
    myregexobj = re.compile(myregstring)
    # Now search
    searchstat = myregexobj.search(filename)
    if searchstat:
        print 'Regex', filename

It is called like this:

if __name__ == '__main__':
    fileextensions = ['\.doc', '\.o', '\.out', '\.c', '\.h']
    try:
        currentdir = os.getcwd()
    except OSError:
        print 'Error occured while getting current directory'
        sys.exit(1)
    for myfiles in os.listdir(currentdir):
        matchextname(fileextensions, myfiles)

Could you please review the code and suggest if there is any better way of doing this, or share any other comments related to errors/exception handling which are missing - or anything else in terms of logic?

Tweeted twitter.com/#!/StackCodeReview/status/273875730516017153
correct the formatting
Source Link
Gareth Rees
  • 50.1k
  • 3
  • 130
  • 211

I have written a pyhthon function, which matches all files in the current directory with a list of extension names.It is working correctly.

import os, sys, time, re, stat

def matchextname( extnames, filename):
#Need    # Need to build the regular expression from the list
    myregstring = ""
    for index in range(len(extnames)):
    #r1    # r1 union r2 and  so on operator is pipe(|)
    #$    # $ is to match from the end
        if index <  len(extnames) - 1:
            myregstring =  myregstring + extnames[index] + '$' + '|'
        else:
            myregstring = myregstring + extnames[index] + '$'
#getting    # getting regexobject
    myregexobj = re.compile(myregstring)
#Now    # Now search
    searchstat = myregexobj.search(filename)
    if searchstat:
        print 'Regex', filename

It is called like this,:

if __name__ == '__main__':
 
    fileextensions = ['\.doc', '\.o', '\.out', '\.c', '\.h']
    try:
        currentdir = os.getcwd()
    except OSError:
        print 'Error occured while getting current directory'
        sys.exit(1)
    for myfiles in os.listdir(currentdir):
        matchextname(fileextensions, myfiles)

Could you please review the code and suggest if there is any better way of doing this or could you please share any other comments relatedrelated to errors/exception handlings which are missing or anything else in terms of logic.?

I have written a pyhthon function, which matches all files in the current directory with a list of extension names.It is working correctly.

import os, sys, time, re, stat

def matchextname( extnames, filename):
#Need to build the regular expression from the list
myregstring = ""
for index in range(len(extnames)):
    #r1 union r2 and  so on operator is pipe(|)
    #$ is to match from the end
    if index <  len(extnames) - 1:
        myregstring =  myregstring + extnames[index] + '$' + '|'
    else:
        myregstring = myregstring + extnames[index] + '$'
#getting regexobject
myregexobj = re.compile(myregstring)
#Now search
searchstat = myregexobj.search(filename)
if searchstat:
    print 'Regex', filename

It is called like this,

if __name__ == '__main__':
 
fileextensions = ['\.doc', '\.o', '\.out', '\.c', '\.h']
try:
    currentdir = os.getcwd()
except OSError:
    print 'Error occured while getting current directory'
    sys.exit(1)
for myfiles in os.listdir(currentdir):
    matchextname(fileextensions, myfiles)

Could you please review the code and suggest if there is any better way of doing this or could you please share any other comments related to errors/exception handlings which are missing or anything else in terms of logic.

I have written a pyhthon function, which matches all files in the current directory with a list of extension names.It is working correctly.

import os, sys, time, re, stat

def matchextname(extnames, filename):
    # Need to build the regular expression from the list
    myregstring = ""
    for index in range(len(extnames)):
        # r1 union r2 and  so on operator is pipe(|)
        # $ is to match from the end
        if index <  len(extnames) - 1:
            myregstring =  myregstring + extnames[index] + '$' + '|'
        else:
            myregstring = myregstring + extnames[index] + '$'
    # getting regexobject
    myregexobj = re.compile(myregstring)
    # Now search
    searchstat = myregexobj.search(filename)
    if searchstat:
        print 'Regex', filename

It is called like this:

if __name__ == '__main__':
    fileextensions = ['\.doc', '\.o', '\.out', '\.c', '\.h']
    try:
        currentdir = os.getcwd()
    except OSError:
        print 'Error occured while getting current directory'
        sys.exit(1)
    for myfiles in os.listdir(currentdir):
        matchextname(fileextensions, myfiles)

Could you please review the code and suggest if there is any better way of doing this or could you please share any other comments related to errors/exception handlings which are missing or anything else in terms of logic?

Source Link

Python function - To match filenames with extension names

I have written a pyhthon function, which matches all files in the current directory with a list of extension names.It is working correctly.

import os, sys, time, re, stat

def matchextname( extnames, filename):
#Need to build the regular expression from the list
myregstring = ""
for index in range(len(extnames)):
    #r1 union r2 and  so on operator is pipe(|)
    #$ is to match from the end
    if index <  len(extnames) - 1:
        myregstring =  myregstring + extnames[index] + '$' + '|'
    else:
        myregstring = myregstring + extnames[index] + '$'
#getting regexobject
myregexobj = re.compile(myregstring)
#Now search
searchstat = myregexobj.search(filename)
if searchstat:
    print 'Regex', filename

It is called like this,

if __name__ == '__main__':

fileextensions = ['\.doc', '\.o', '\.out', '\.c', '\.h']
try:
    currentdir = os.getcwd()
except OSError:
    print 'Error occured while getting current directory'
    sys.exit(1)
for myfiles in os.listdir(currentdir):
    matchextname(fileextensions, myfiles)

Could you please review the code and suggest if there is any better way of doing this or could you please share any other comments related to errors/exception handlings which are missing or anything else in terms of logic.