Ian Fiddes
Ian Fiddes

Reputation: 3011

subprocess Popen returns an empty string as last list member on communicate()[0].split('\n')

When I call a program with subprocess.Popen as the following:

input_file_new = subprocess.Popen('''grep -v '#' {} | cut -f 1,2,3,4,5,6,7,8 | \
        intersectBed -a stdin -b {} -wo | sort -k 1,1 -k 2,2n |uniq'''.format(infile,\
        bound_motif),shell=True,stdout=subprocess.PIPE).communicate()[0].split('\n')

I get a list of all of the lines from the output generated, with one extra empty string member as the last list item. I can remove it with del but that seems clunky. How can I avoid the extra empty string?

Upvotes: 1

Views: 559

Answers (1)

Don Question
Don Question

Reputation: 11624

change .split('\n') into

rstrip('\n').split()

The last empty string is probably just a trailing newline!

Upvotes: 2

Related Questions