如何将subprocesss.check_output()转换为字符串列表?

时间:2016-12-19 21:11:48

标签: python subprocess

当我运行此子流程cmd时,返回的类型是一个字符串,即使有5行打印到控制台

s3_ls = subprocess.check_output(["aws", "s3", "ls", s3_loc, "-- 
recursive", "--profile", "RoleName"])

2016-11-17 23:34:45          0 dtop_dir/fldr_01/fldr_02/holding/date4y2m/category
2016-11-17 23:34:46          0 dtop_dir/fldr_01/fldr_02/holding/date4y2m/entity.01/
2016-12-05 17:37:25     234059 dtop_dir/fldr_01/fldr_02/holding/date4y2m/entity.01/entity.01.csv.gz
2016-11-17 23:34:47          0 dtop_dir/fldr_01/fldr_02/holding/date4y2m/entity.02/
2016-12-05 17:37:31     109015 dtop_dir/fldr_01/fldr_02/holding/date4y2m/entity.02/entity.02.csv.gz

如何处理此输出,因此它是一个包含5个字符串的列表,而不仅仅是一个很长的字符串?我尝试将它包装在列表中(我的子进程cmd),但这只是将所有内容拆分为单个字符元素。

我想要做的是,在运行上面的python子进程cmd之后返回一个文件列表

2 个答案:

答案 0 :(得分:3)

分裂它怎么样?

s3_ls.split('\n')

答案 1 :(得分:0)

我不确定如何更改s3命令给出的输出,但是输出是一个字符串。您可以通过执行以下操作将其拆分为文件名或整行:

>>> re.findall(r'dtop.+', s)
['dtop_dir/fldr_01/fldr_02/holding/date4y2m/category', 'dtop_dir/fldr_01/fldr_02/holding/date4y2m/entity.01/', 'dtop_dir/fldr_01/fldr_02/holding/date4y2m/entity.01/entity.01.csv.gz', 'dtop_dir/fldr_01/fldr_02/holding/date4y2m/entity.02/', 'dtop_dir/fldr_01/fldr_02/holding/date4y2m/entity.02/entity.02.csv.gz']

或者:

>>> re.split(r'\n', s) # or s.split('\n') would be the same here
['2016-11-17 23:34:45          0 dtop_dir/fldr_01/fldr_02/holding/date4y2m/category', '2016-11-17 23:34:46          0 dtop_dir/fldr_01/fldr_02/holding/date4y2m/entity.01/', '2016-12-05 17:37:25     234059 dtop_dir/fldr_01/fldr_02/holding/date4y2m/entity.01/entity.01.csv.gz', '2016-11-17 23:34:47          0 dtop_dir/fldr_01/fldr_02/holding/date4y2m/entity.02/', '2016-12-05 17:37:31     109015 dtop_dir/fldr_01/fldr_02/holding/date4y2m/entity.02/entity.02.csv.gz']