0

Pretty new to python. I'm trying to run a loop to convert hgt files to sdf files using the following script:

import os
for root, dirs, files in os.walk("."):
    for name in files:
        os.system("srtm2sdf-win.exe *.hgt")

But the script only deals with the very first file. Here is the output:

Reading N10W110.hgt...
Writing 10x11x109x110.sdf... Done!
Reading N10W110.hgt...
Writing 10x11x109x110.sdf... Done!
Reading N10W110.hgt...
Writing 10x11x109x110.sdf... Done! 
Erico
  • 1
  • 3
    Welcome to SO! `os.system("srtm2sdf-win.exe *.hgt")` is always the same command--did you mean to use `name`? – ggorlen Oct 16 '20 at 17:05
  • I was trying to figure how to use name. Basically the files all end in hgt and the file conversion is executed by running the command srtm2sdf-win.exe – Erico Oct 16 '20 at 19:00
  • How about `os.system(f"srtm2sdf-win.exe {name}.hgt")` if you're on Python 3.6+? – ggorlen Oct 16 '20 at 19:03
  • Version 3.3.2, so did not work. I can upgrade to 3.6 and try – Erico Oct 16 '20 at 19:10
  • Nah, just use `os.system("srtm2sdf-win.exe %s.hgt" % name)`. The question seems to reduce down to "how do I concatenate/interpolate strings in Python pre 3.6?". – ggorlen Oct 16 '20 at 19:12
  • Does this answer your question? [How can strings be concatenated?](https://stackoverflow.com/questions/2711579/how-can-strings-be-concatenated) – ggorlen Oct 16 '20 at 19:15
  • Thanks for the help. I had to modify it slightly. The code that worked is: `import os for root, dirs, files in os.walk("."): for name in files: if name.endswith(".hgt"): os.system("srtm2sdf-win.exe %s" % name)` – Erico Oct 16 '20 at 19:23
  • Nice to hear and good work--I forgot that `name` has the extension attached. Feel free to post a [self answer](https://stackoverflow.com/help/self-answer). – ggorlen Oct 16 '20 at 19:25

0 Answers0