For loop is throwing "OSError: [Errno 2] No such file or directory" after first iteration it is not iterating 2nd item in the list

1021 views python-2.7

I have one list with directories. I need to go each directory of a list and execute some command. like this I need to process entire list. But after first iteration it is throwing OSError. Please find the below code and help me.

CODE: ls_2 = ['build', 'flexiserver', 'fvntools', 'gbuild', 'sample'] for dir in ls_2: print ("Inside for loop") os.chdir(dir) ls_2 = os.listdir('.') print ("Inside dir %s %s" %(dir, ls_2))'')

answered question

Looks like what one of those folders doesn't contain a

Actually I am exporting the before ierating the loop so that it will available to execute in all directories. CODE: os.chdir('scm_scripts') os.system('export PATH=$PATH:$(pwd)') os.chdir('..') print ("After export scm_scripts path is" , os.getcwd()) for dir in ls_2: print ("Inside for loop")

os.system('export PATH=$PATH:$(pwd)') does nothing.

1 Answer


Are the folder nested inside one another?

One possible reason is all these folders inside the same folder. In that case, after you get into build, you want to go one level back before you access flexiserver.

posted this

Have an answer?


Please login first before posting an answer.