3

In my Python script I'm trying to execute next code:

import subprocess
subprocecss.call("xrdb -load ~/.XDefaults")

but it falls with error: "No such file or directory", although it works when I paste the same code to terminal. I also tryed os.system(...) with import os, I tried it with "xrdb -merge ~/.XDefaults", I tried to delete ~/ from command, I even tried to change "" to '', no way. What I'm doing wrong?

Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162

2 Answers2

4

You need to use shell=True or add your file with full path :

subprocecss.call("xrdb -load ~/.XDefaults",shell=True)

from python wiki :

On Unix with shell=True, the shell defaults to /bin/sh. If args is a string, the string specifies the command to execute through the shell

On Windows with shell=True, the COMSPEC environment variable specifies the default shell. The only time you need to specify shell=True on Windows is when the command you wish to execute is built into the shell (e.g. dir or copy).

Community
  • 1
  • 1
Mazdak
  • 105,000
  • 18
  • 159
  • 188
  • I'm sorry for offtop, but maybe you can prompt me some messagebox where I can set foreground and background parameters – banan-olivka Dec 28 '14 at 12:29
  • @banan-olivka .. its OK . if you want so there is better choices like `subpocecess.popen()` – Mazdak Dec 28 '14 at 12:31
  • I suppese, subprocess.popen(message='...', title='...', foreground='#000000', background='#ffffff'), but it raises error: '__init__() got unexpected keyword argument 'foreground' ' – banan-olivka Dec 28 '14 at 12:48
  • @banan-olivka you couldn't pass Arbitrary argument ... you can pass your arguments in an iterable like list , pleas read the document https://docs.python.org/2/library/subprocess.html#using-the-subprocess-module – Mazdak Dec 28 '14 at 12:57
1

Note that since subprocess.call by default doesn't inherit your environment the value for ~ is not defined so you either need to pass the shell=True flag, (potentially dangerous), or give the absolute path for ~/.XDefaults by typing it in or using os.path.expanduser('~/.XDefaults') to get it, (as suggested by falstru).

Steve Barnes
  • 27,618
  • 6
  • 63
  • 73