0

I want to pass arguments to an rsync subprocess from a list (or string) but can't find any way to do it without specifying each list item. ie this works

args = ['--progress', '-avh']
subprocess.run(['rsync', args[0],args[1],loaded_prefs['src_dir'],loaded_prefs['dst_dir']])

but this doesn't

args = '--progress -avh'
subprocess.run(['rsync', args,loaded_prefs['src_dir'],loaded_prefs['dst_dir']])

or this

args = ['--progress', '-avh']
subprocess.run(['rsync', ','.join(args),loaded_prefs['dst_dir']])

Any help would be much appreciated

Chris Barrett
  • 571
  • 4
  • 23
  • `['rsync'] + args + [loaded_prefs['src_dir'], loaded_prefs['dst_dir']]` is how people might have done it in older versions of Python. – Charles Duffy Jun 05 '23 at 19:53

1 Answers1

3

--progress and -avh need to be separate elements of the list you pass to subprocess.run(). You're combining them into a single string. Keep it as a list, and then spread the list with *.

args = ['--progress', '-avh']
subprocess.run(['rsync', *args,loaded_prefs['src_dir'],loaded_prefs['dst_dir']])
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • This might require a slice, like `*args[:2]`, if the OP is really only taking the first two elements of a longer list. – chepner Jun 05 '23 at 19:53
  • I suppose. I assumed he was trying to generalize, so `args` could be any length and he wouldn't have to specify individual indexes. – Barmar Jun 05 '23 at 19:55