-2

Basically, it's a python code from our collaborator that used to generate mesh, which is developed under Linux environment. I use Cygwin to run this code on windows. The trouble part is as follows. BiV_temp.geo is also a python script. So the command is to substitute the string <> in the script BiV_temp.geo with a predefined number and the file names.

os.system('cp BiV_fiber.geo BiV_temp.geo')
cmd = "sed -i 's/<<Meshsize>>/"+"%5.2f"%(meshsize)+"/g' BiV_temp.geo"
os.system(cmd)
cmd = "sed -i 's/<<LVfilename>>/"+"\"%s\""%(LVendocutfilename)+"/g' BiV_temp.geo"
os.system(cmd)
cmd = "sed -i 's/<<RVfilename>>/"+"\"%s\""%(RVendocutfilename)+"/g' BiV_temp.geo"
os.system(cmd)
cmd = "sed -i 's/<<Epifilename>>/"+"\"%s\""%(epicutfilename)+"/g' BiV_temp.geo"
os.system(cmd)
cmd = "gmsh -3 BiV_temp.geo -o %s"%(mshfilename)
os.system(cmd)
cmd = "rm BiV_temp.geo"
os.system(cmd)
H.Zou
  • 9
  • 2
  • 2
    Are you sure it was a Python traceback, and not a sed error? I've never seen an error message like that. – TigerhawkT3 Jan 15 '16 at 02:41
  • Probably. As I am all new to python and unix, so it may be the sed error... – H.Zou Jan 15 '16 at 02:47
  • 1
    @H.Zou Please paste your attempt at running the script and the ensuing error to your question so we can be sure. – Michael Recachinas Jan 15 '16 at 02:50
  • Try `print("sed -i 's/<>/"+"%5.2f"%(meshsize)+"/g' BiV_temp.geo")`, exit python, then paste the result on the command line. This works for me with a dummy file using bash on linux mint, but maybe it angers your shell or your `sed`. You could replace `sed` with `echo` to see if you have the same problem. – tdelaney Jan 15 '16 at 03:24
  • 1
    You don't tell which line causes the error (there are multiple lines with `<<`). You also don't provide the input file for other people to attempt, or (preferably) a small input file that still causes your error. That makes it a lot harder to help you with. –  Jan 15 '16 at 03:42
  • @tdelaney The command can be executed seperately on the command line. With replacing sed with echo it doesn't help. So not sure why in python script it can not work. – H.Zou Jan 15 '16 at 04:16
  • @Evert All the << cause this error. – H.Zou Jan 15 '16 at 04:18
  • 1
    This is crazy. If your entire program consists of shell script, write a shell script, not a Python wrapper to start a new shell for each piece of shell script. Also notice that `sed` is a scripting language, so you can [combine all of these `sed` snippets into one](http://stackoverflow.com/questions/7657647/combining-2-sed-commands); then copying the source file to a temporary file and doing inline substitutions `-i` no longer makes any sense (if it ever did). – tripleee Jan 15 '16 at 04:19
  • You can possibly execute it fine in your terminal, because `os.system()` does not equal your terminal. Shell, envvars etc are different (just run e.g. `os.system('echo $0')` and `os.system('env')` from a Python prompt). Possibly, that could cause your problem. –  Jan 15 '16 at 04:24

2 Answers2

0

The sane solution is for your "collaborator" to write Python code which allows you to pass in these things as parameters to a function call.

tripleee
  • 175,061
  • 34
  • 275
  • 318
-1

you are trying to executing command code in python? and i notice in "%5.2f"%(meshsize) you wrote %5, for adding text you should add %sand also avoid putting plus sign, it make it really hard to read and messy, i would write all in one line like this:

meshsize = 100
cmd = "sed -i 's/<<Meshsize>>/%s5.2f/g' BiV_temp.geo" %meshsize
print cmd

if your meshsize is a list of x,y,z then write it like this:

meshsize = (100,50,20)
cmd = "sed -i 's/<<Meshsize>>/%s,%s,%s5.2f/g' BiV_temp.geo" %meshsize
print cmd

hope that help.

Bear
  • 550
  • 9
  • 25
  • 1
    This is just a comment about style, it doesn't fix the problem. – tdelaney Jan 15 '16 at 03:38
  • Which "you" do you mean? He is trying to replace "<>" with a floating point number using `sed`. He doesn't need the `%s` stuff you are doing in your second incorrect example. – tdelaney Jan 15 '16 at 03:45
  • @tdelaney: it make it readable, which is cunsider one of the problems, so people wont skip reading it, you type your code in notepad don't you? :D – Bear Jan 15 '16 at 03:50
  • @tdelaney when i read the question it was only 2 lines of code, but nothing else, so i just suggest to write as such way. what's wrong with you? – Bear Jan 15 '16 at 03:52
  • 1
    Hi guys, don't be angry. All the suggestions are appreciated. @BernardRouhi – H.Zou Jan 15 '16 at 04:19
  • @H.Zou yeah, i actually never knew `%5.2f` exist, i usually add as String `%s` and number as `%d`, even that everything will be string in `cmd` variable anyway, but that was something for me to take. – Bear Jan 15 '16 at 04:32
  • What do you mean, what's wrong with me? This isn't an answer and is incorrect to boot. Low quality answers get dinged... get used to it. – tdelaney Jan 15 '16 at 04:45