0

I have a script which runs copy command to copy files and directories. Since, i have arguments stored in variables.

copy_cmd = ['Lang=en_US' , '/bin/cp' , '-r' , '-v']

Option['source']

Option['destination']

I'm passing the value of Option['source'] & Option['destination'] at run time. I'am unable to perform the command and getting....

child exception
AttributeError: list object has no attribute rfind

The command which i'am using is

copy_pid = subprocess.Popen([copy_cmd , Option['source'] , '/.' , 'Option['destination']'] , stdin = subprocess.PIPE , stdout = subprocess.PIPE)
miszcz2137
  • 894
  • 6
  • 18
  • What is Option? Is it a dict? Is it a list? If you do `print(type(Option['source']))` what do you get? And if you did `print(Option['source'])` what do you see? – DUDANF Mar 09 '20 at 09:48
  • 1
    Basically, you need to make sure everything you're passing in is correct. This command works: `subprocess.Popen(["echo", "hello", "world"])` And so you need to pass a list in. Also, you could attempt `shell=True` if you require your command to be run in a shell. Please give us more information. – DUDANF Mar 09 '20 at 09:55

1 Answers1

0

You need to pass lis of strings yet copy_cmd is already a list. So you meed to concatenate it and not insert as an element.

copy_pid = subprocess.Popen(copy_cmd + [Option['source'] , '/.' , Option['destination']] , stdin = subprocess.PIPE , stdout = subprocess.PIPE)

Also I don't understand what are those Option for so I just left them as they were.

miszcz2137
  • 894
  • 6
  • 18
  • Wouldn't this literally just work: `copy_cmd = ['Lang=en_US' , '/bin/cp' , '-r' , '-v', Option["source"], Option["destination"]]`? :/ – DUDANF Mar 09 '20 at 10:06
  • 1
    For this case yes. But I think that is not exact answer and if the question is simplified then not exact answer may not be applicable. So I don't try guessing authors intentions I just try to anwser the question asked the best I can. – miszcz2137 Mar 09 '20 at 10:09
  • copy_cmd = ['Lang=en_US' , '/bin/cp' , '-r' , '-v', Option["source"], Option["destination"]] I already tried this way, but it gets me an error. Below is the error: raise child exception OSError: [Errno 2] No such file or directory However, i tried this copy_cmd = [ '/bin/cp' , '-r' , '-v', Option["source"], Option["destination"]] and it worked fine. Option["source"] and Option["destination"]] contains the path of different directories in string format. – deepak goswami Mar 11 '20 at 05:53
  • It did n't work because it tried to run file `'Lang=en_US'`. Why did you even add `'Lang=en_US'`? – miszcz2137 Mar 11 '20 at 10:32