Python system call (os.system) doesn't work as expected with the same string in two cases

Viewed 40

I'm doing the exact same thing in these two cases, but case 1 doesn't work. Anyone knows why Python behaves this way?

# Case 1
setup_folder = "/home/svimal/Data/ABoVE/100_catchments/VIC_setup/05MG001/"
os.system('../VIC_4.1.2.k.Purdue/vicNl -g  ' + setup_folder + "/Global_Param")
# Case 2
os.system("../VIC_4.1.2.k.Purdue/vicNl -g /home/svimal/Data/ABoVE/100_catchments/VIC_setup/05MG001/GlobalParam")
1 Answers

This snippet will create an argument /home/svimal/Data/ABoVE/100_catchments/VIC_setup/05MG001//Global_Param - note the // before "Global_Param".

You could use os.path.join to avoid having to deal with these slashes yourself:

arg = os.path.join('/home/svimal/Data/ABoVE/100_catchments/VIC_setup/05MG001/', 'Global_Param')
os.system('../VIC_4.1.2.k.Purdue/vicNl -g  ' + arg)
Related