I have a script that compiles few programs and puts the output into a folder, I have a function in my script that iterates and finds all the executables in the folder and adds them into a list,
def _oswalkthrough(direc,filelist=[]):
dirList=os.listdir(direc)
for item in dirList:
filepath = direc+os.sep+item
if os.path.isdir(filepath):
filelist = _oswalkthrough(filepath,filelist)
else:
if ".exe" == item[-4:]:
filelist.append(filepath)
return filelist
This works with out any problem on windows, but when I run this on a mac, I can’t get it to work, of course the compiled files in mac doesn’t end with “.exe”, so that if statement is useless, so I made a list that contains the name of the compiled files and changed the script to the following, but still no result, it adds all the files, including the “.o” files, which I do not want!. I just want the exe? (I don’t know what they are called in mac !).
def _oswalkthrough(direc,filelist=[]):
dirList=os.listdir(direc)
for item in dirList:
filepath = direc+os.sep+item
if os.path.isdir(filepath):
filelist = _oswalkthrough(filepath,filelist)
else:
for file in def_scons_exe:
if file == item[-4:]:
filelist.append(filepath)
return filelist
Looks like the section:
Should be:
This change makes the program look for extensions of any length, not just 4 characters. It also stops looking through the extensions when it finds one that matches.
Also, you should try using the
os.walkfunction to simplify your code as well asos.access([path], os.X_OK)to test for execute permissions.