Python- How can I run executable files in different directories for that directory?


Python- How can I run executable files in different directories for that directory?


Here is my current code:
import csv
def read_file():
# open the file in universal line ending mode
with open('C:UsersDesktopPython FilesNPV_Out4SWAT.csv') as infile:
# read the file as a dictionary for each row ({header : value})
reader = csv.DictReader(infile)
data = {}
for row in reader:
for header, value in row.items():
try:
data[header].append(value)
except KeyError:
data[header] = [value]
return data

import numpy
def get_data(var):
print var
names = data.get(var)
if var == 'Temp_degF':
array = numpy.asarray([float(i) for i in names])
array = numpy.subtract(array, 32)
array = numpy.divide(array, 1.8)
elif var == 'Slr_Langleys':
array = numpy.asarray([float(i) for i in names])
array = numpy.multiply(array, 0.04184)
elif var == 'Wind_mph':
array = numpy.asarray([float(i) for i in names])
array = numpy.multiply(array, 0.44704)
elif var == 'Precip_in_per_y':
array = numpy.asarray([float(i) for i in names])
array = numpy.multiply(array, 0.0695428953)
elif var == 'RH_pct':
array = numpy.asarray([float(i) for i in names])
array = numpy.divide(array, 100)
return array
print array

#Convert to SI units
import os
#generate weather file
def write_weather_file_pcp(num, type, val, n_days):

filename = "./" + str(num)+ "/" + type + "1" + "." +type
with open(filename, "w+") as file:
file.write('Station p426-791,nLati 42.6nLong -79.1nElev 189n')
for day in range(0, n_days):
file.write('000000000' + str(val)+'n')

def write_weather_file_slr(num, type, val, n_days):

filename = "./" + str(num)+ "/" + type + "." +type
with open(filename, "w+") as file:
file.write('Input File slr.slr 6/21/2018 ArcSWAT 2012.10_2.19n')
for day in range(0, n_days):
file.write('000000000' + str(val)+'n')

def write_weather_file_wnd(num, type, val, n_days):

filename = "./" + str(num)+ "/" + type + "." +type
with open(filename, "w+") as file:
file.write('Input File wnd.wnd 6/21/2018 ArcSWAT 2012.10_2.19n')
for day in range(0, n_days):
file.write('0000000000' + str(val)+'n')

def write_weather_file_hmd(num, type, val, n_days):

filename = "./" + str(num)+ "/" + type + "." +type
with open(filename, "w+") as file:
file.write('Input File hmd.hmd 6/21/2018 ArcSWAT 2012.10_2.19n')
for day in range(0, n_days):
file.write('0000000000' + str(val)+'n')

def write_weather_file_tmp1(num, type, val, n_days):

filename = "./" + str(num)+ "/" + type + "1" + "." + type
with open(filename, "w+") as file:
file.write('Station t426-791,nLati 42.6nLong -79.1nElev 189n')
for day in range(0, n_days):
file.write('00000000' + str(val) + "0" + str(val) + 'n')

def main():
global data
data = read_file()
vars = data.keys()
for var in vars:
array = get_data(var)
if var == 'Precip_in_per_y':
for i, value in enumerate(array):
try:
os.mkdir(str(i))
except:
pass
write_weather_file_pcp(i, 'pcp', str(round(value, 1)), 12996)
if var == 'Slr_Langleys':
for i, value in enumerate(array):
try:
os.mkdir(str(i))
except:
pass
write_weather_file_slr(i, 'slr', str(round(value, 3)), 12996)
if var == 'Wind_mph':
for i, value in enumerate(array):
try:
os.mkdir(str(i))
except:
pass
write_weather_file_wnd(i, 'wnd', str(round(value, 3)), 12996)
if var == 'RH_pct':
for i, value in enumerate(array):
try:
os.mkdir(str(i))
except:
pass
write_weather_file_hmd(i, 'hmd', str(round(value, 3)), 12996)
if var == 'Temp_degF':
for i, value in enumerate(array):
try:
os.mkdir(str(i))
except:
pass
write_weather_file_tmp1(i, 'tmp', str(round(value, 1)), 12996)


#####################################################################

if __name__ == '__main__':
main()

import shutil
src_files = os.listdir('C:UsersDesktopPython FilesOriginalsTxtInOut')

for j in range(0, 1000):

full_dest_name = os.path.join( 'C:UsersDesktopPython Files', str(j))

for file_name in src_files:
full_file_name = os.path.join('C:UsersDesktopPython FilesOriginalsTxtInOut', file_name)
if (os.path.isfile(full_file_name)):
shutil.copy(full_file_name, full_dest_name)

target_dir = ('C:UsersDesktopPython Files')
wd = os.path.dirname(os.path.abspath(__file__))


os.chdir(target_dir)
os.system('SWAT2012.exe') #or whatever your executable is named
os.chdir(wd)



I'm not sure how to run an executable file that has been copied into each of the generated 1000 folders. The input parameters for the executable has been copied along with it but is generated differently as a result of write_weather_file.
I've been using os.system and os.chdir but I'm not sure how to loop it so that it'll run the executable for different directories and more than once.



How can I:



(part 2). Run the executable file, write the results to another file, delete the folder containing the executable and the inputs, and then run the executable file in the next folder?



Thank you!









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages