I'm trying to run a bash command with Python3 on Ubuntu. Here is the code (sc.py)
import subprocess
path = '/home/user2/*'
result = subprocess.run(['ls', path], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print(result.stdout.decode("utf-8"))
print(result.stderr.decode("utf-8"))
Both python3 sc.py
and sudo python3 sc.py
give
ls: cannot access '/home/u0502210301/*': No such file or directory
However, directly running the command ls /home/user2/*
with bash outputs as expected.
All the tests above are run with a sudo account user1
.
Could someone give me a hint about this?