0

With the help of the nice answers from subprocess.run() fails when using a glob expression as an argument; works fine in shell I learned a little bit about glob expansion.

However, I have no idea how to use cp * bash command with subprocess.run

This command copies all the files in /home/user2/ to user2 folder in current directory.

cp -rf /home/user2/* ./user2

I tried to do the same job with the following code

import subprocess
import glob
path = '/home/user2/*'
cmd = ['cp', '-rf']
cmd.extend(glob.glob(path))
cmd.extend('./user2/')
result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print(result.stdout.decode("utf-8"))
print(result.stderr.decode("utf-8"))

though, it doesn't work. Could someone give me a hint about this?

JJJohn
  • 647
  • 4
  • 15

0 Answers0