0

How should I use os.system to run script for several directories. I tried this:

listofdirnumbers = [1, 2, 3, 4, 5]
for i in range(len(listofdirnumbers)):
    os.system("script.py 'arg1 "mydir_%d"%i arg2 '")

But it gave me the error of invalid syntax.

Another question how does it work if I want to input several arguments - is this correct?

(os.system("script.py 'arg1 arg2 arg3'"))
user3041107
  • 123
  • 2
  • 13
  • What is `mydir_`? is that supposed to be inside the string? Double check your quote marks. – Kevin Jun 18 '14 at 14:00
  • Could you give an example of what you mean run a script for several directories as well? Is a directory/directory's contents the input? – GleasonK Jun 18 '14 at 14:03
  • mydir_ is some directory so it should look like: mydir_01 then mydir_02 etc. – user3041107 Jun 18 '14 at 14:04
  • yes, in these directories there are my input files. So the script takes one directory and all the files inside this directory ... runs ... and then it skips to another directory. – user3041107 Jun 18 '14 at 14:05
  • ... I have no idea what you are doing with `listofdirnumbers`... why do you even define it? – Bakuriu Jun 18 '14 at 14:11
  • btw: use args without quotas `os.system("script.py arg1 arg2 arg3")`. With quotas in `os.system("script.py 'arg1 arg2 arg3' ")` `script.py` will treat `'arg1 arg2 arg3'` as one long argument. – furas Jun 18 '14 at 14:11

2 Answers2

0

The command you wish to execute including arguments should be passed as a string. There is no such concept as nested string like "script.py 'arg1 "mydir_%d"%i arg2 '" in Python.

listofdirnumbers = [1, 2, 3, 4, 5]
for i in range(len(listofdirnumbers)):
    os.system("script.py arg1 mydir_%d arg2" % i)

Multiple arguments can be passed like

os.system("script.py arg1 arg2 arg3")
Parth
  • 729
  • 8
  • 23
  • ok this worked finally, thanks, I made some stupid things on the way and these quotes are very confusing for me as a non-programmer. – user3041107 Jun 18 '14 at 14:42
0

The problem are the double quotes inside your double-quoted string. You probably wanted to write something like:

os.system("""script.py 'arg1 "mydir_%d" arg2 '""" % i)

Or, escaping the double quotes:

os.system("script.py 'arg1 \"mydir_%d\" arg2 '" % i)

Even though the double quotes are actually useless in this circumstance...

I don't really know why you are adding the single quotes inside that command. The single-quote delimited string would be considered a single argument. In the example script.py will receive one argument of the form arg1 "my_dir_N" arg2 where N is an integer. If you want to pass more than one argument to the program don't group them with single quotes.

Also you should avoid os.system altogether. The subprocess module provides a much safer and more flexible interface. The code using subprocess would be:

import subprocess

for i in listofdirnumbers:
    subprocess.call(['script.py', 'arg1', 'my_dir_%d' % i, 'arg2'])

(this will provide three arguments to script.py).

Bakuriu
  • 98,325
  • 22
  • 197
  • 231