-1

I have mada a config file for my python program.. The config file contains my variable which uses the 'i2cset' module.. It is as follows: This is my config file called'try.ini'

 [new]
  a = ('i2cset', '-y', '0', '0x20', '0x14', '0x01')

I read it from my main code as follows:

from ConfigParser import SafeConfigParser
  parser=SafeConfigParser()
  parser.read('try.ini')

  def set21(tog=[0]):
      tog[0]= not tog[0]
      if tog[0]:
          set0.config(text='p21_1')
          A=parser.get('new', 'a')
          subprocess.call(''.join(A), shell=True)
      else:
          set0.config(txt='p21_0')

But everytime I execute the main program I get an error saying :

/bin/sh: 1: 'i2cset ': not found.

I dont know why does this happen.. When I run the program without the config file it executes completley fine... Can somebody please help..

Kind Regards, Namita.

Goldengirl
  • 957
  • 4
  • 10
  • 30
  • 1
    How do you assign 'a' in your code? – RvdK Apr 13 '15 at 07:31
  • Sry! I forgot to mention that.. I have added it and updated my question.. Please have a look.. – Goldengirl Apr 13 '15 at 07:38
  • 3
    That can't be your code. There's nothing called `ConfidParser`, you can't assign to `A` and then use it as `a`, and you can't indent things randomly and expect them to work. Please show us a [minimal, complete, verifiable example](http://stackoverflow.com/help/mcve), not just something vaguely like your code that may or may not even contain the error you need help with. – abarnert Apr 13 '15 at 07:38
  • As a side note, why would you want to `call(' '.join(a), shell=True)` instead of just leaving it as a list (so you don't need to worry about spaces or other weirdnesses—and so you don't need the shell) and just doing `call(a)`? – abarnert Apr 13 '15 at 07:39
  • 1
    Random wild guess #1: Your actual code does the `' '.join(…)` twice; the first one joins the tuple into a string with a space between each element, and then the second one joins the string into a string with a space between each character. Fix: don't do that. – abarnert Apr 13 '15 at 07:41
  • Random wild guess #2: You're on Windows, and you accidentally saved your config file in UTF-16-LE instead of UTF-8, and those apparent spaces in the error are actually NUL bytes. – abarnert Apr 13 '15 at 07:41
  • I have updated my code again guys.. I added in the whole function..@abarnert: I rectified the mistake of ' '.join(...) twice.. :) – Goldengirl Apr 13 '15 at 07:50
  • @abarnert: It was a typing mistake.. Instead of ConfigParser I typed in ConfidParser.. And I have used the variable 'a' in my config file.. And to join it and access the value (by reading from the config file) I used the variable 'A'. – Goldengirl Apr 13 '15 at 08:20
  • @NamitaRaju: But that typing mistake is not in your real code, or you wouldn't have gotten this far. And likewise, if you used the variable `A`, then `' '.join(a)` won't work; that must be _another_ typing mistake that isn't in your real code, but is in what you posted here. So you haven't shown us either your real code, or an MCVE, making your question harder to answer. Why make it harder for people who are trying to help you? – abarnert Apr 13 '15 at 08:33
  • @abarnert: I don't mean to be offensive here, but that is my real code. My whole program is for about 20 other such toggle buttons.. It does not make sense to post the whole thing. Thus I just posted one single function.... I kept geetting the error because I was trying to read 'a' which was assigned as a string , as a variable.. I know you guys are trying to help me, so why will I want to make it difficult for you? Any way thank you for all the help.. Issue has been solved – Goldengirl Apr 13 '15 at 08:42
  • @NamitaRaju: No, this really is not your real code. You had to have multiple errors pointed out to you and edit the question multiple times, and what you've posted _still_ just raises an immediate `IndentationError`. I linked to the MCVE help page for a reason. Please read it. – abarnert Apr 13 '15 at 09:17

1 Answers1

0

Your mistake is in the syntax of the .ini file.

[new]
a = ('i2cset', '-y', '0', '0x20', '0x14', '0x01')

You expect the variable a to be read as a tuple, whereas it is actually a string value containing "('i2cset', '-y', '0', '0x20', '0x14', '0x01')".

phuclv
  • 37,963
  • 15
  • 156
  • 475
Hans Then
  • 10,935
  • 3
  • 32
  • 51
  • Thank you for that response.. Then if not that way how can I access this value of a from my config file ? – Goldengirl Apr 13 '15 at 08:11
  • You have options. One way would be to put `a=i2cset -y 0 0x20 0x14 0x01` in your config file and use `subprocess.call(a, shell=True)` in your code. – Hans Then Apr 13 '15 at 08:21
  • Thank you so much.. Now it seems to work.. The mistake was as you said I was trying to access a string as a variable.. Thank you :) – Goldengirl Apr 13 '15 at 08:29
  • Or you could use `ast.literal_eval` to parse the string into a tuple. But that seems overly complicated for no good reason… – abarnert Apr 13 '15 at 08:33