When we want to use Unix command in python we can directly use os.system()
to realize it. However, if we only want to return the output as a string, for example, return ls
file names into a string, we need to use subprocess.check_out
instead.
To note that, the output from subprocess.check_out()
is a bytes object instead of a string, thus we need to further decode
to transform into a string.
fileList = subprocess.check_output('ls someFolder/*', shell=True).decode('utf-8').strip().split('\n')
Source:
https://stackoverflow.com/questions/606191/convert-bytes-to-a-string