# Copies files in a specified subdirectory to other subdirectories, and runs them. import os import shutil # Change to directory where script is located abspath = os.path.abspath(__file__) dname = os.path.dirname(abspath) os.chdir(dname) # Specify the subdirectory to copy the files from sourcedir = 'CopySource' # Get all the subdirectories. Copy to all of these by default, can manually add exceptions if required. dirlist = next(os.walk('.'))[1] # Get a list of all the files in the source directory filelist = os.listdir(dname+'/'+sourcedir) for dirc in dirlist: # Add exceptions to directories here, if necessary. These are the destination directories. # Also, don't copy to the directory we want to copy from ! if 'CopySource' not in str(dirc): destination = dname+'/'+dirc+'/' for filename in filelist: shutil.copy(dname+'/'+sourcedir+'/'+filename, destination) # Now go through each of the destination directories and run the program we want to run for dirc in dirlist: # Add exceptions to directories here, if necessary. These are the destination # directories. if 'CopySource' not in str(dirc): os.chdir(dname+'/'+dirc) # Specific example of compiling a particular Fortran code os.system('gfortran -o massposvelacc massposvelacc.f') # We could then run it just by specifying the name of the program to execute os.system('./massposvelacc')