Traversing directories and performing command prompt commands in each with Python -



Traversing directories and performing command prompt commands in each with Python -

in windows 7 pro, need traverse directories @ given location (in case z:\) , perform command prompt commands in each using python. seems should straightforward os.walk(), nil i've tried far has worked--the closest i've gotten have commands infinitely loop desktop (that script below). need accomplish task?

import os root, dirs, files in os.walk("z:/"): dir in dirs: os.system('cd z:\\' + dir) os.system('git init') os.system('git add together .') os.system('git commit -m \"initial\"')

when run os.system('cd wherever') creating new command shell has own thought of current directory.

when shell exits, parent process not retain current directory of kid process, subsequent os.system() calls don't see alter in current directory.

the right way alter current directory in parent process (your script) inherited kid process:

import os root, dirs, files in os.walk("z:/"): dir in dirs: os.chdir('z:\\' + dir) os.system('git init') os.system('git add together .') os.system('git commit -m \"initial\"')

python

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -