2

I am writing a script to sort a file based on a specific column. I tried calling the 'sort' Linux command for this. The code I am using is:

from subprocess import 
path_store = /homes/varshith/maf
input = path_store
field = "-k2"
store_output_in_new_file = ">"
new_path = path_store + "_sort.bed"
sorting = Popen(["sort", field, input, append, new_path], stdout=PIPE)

But this doesn't work properly. Thanks in advance for helping.

1
  • btw, ">" does not append, it overwrites Commented Jan 21, 2015 at 14:49

3 Answers 3

3

Use communicate to get the output:

from subprocess import PIPE,Popen
sorting = Popen(["sort", field, output, append, new_path], stdout=PIPE)
out, err = sorting.communicate()  
print out

Or just use check_output for python >= 2.7:

sorting = check_output(["sort", field, output, append, new_path])

If you want to just write the sorted contents you can redirect stdout to a file object:

output = "path/to/parentfile"
cmd = "sort -k2 {}".format(output)
with open(new_file,"w") as f:
    sorting = Popen(cmd.split(),stdout=f)
Sign up to request clarification or add additional context in comments.

16 Comments

I get an error like: sort: stat failed: >: No such file or directory
add your full input to your question, my answer relies on you actually passing the correct commands
path_store is the variable that contains the path to the file as a string
And I am actually trying to write the output from sort into the new file.
ok, as it stands you are trying to sort > not the file, one sec I will edit
|
0

First of all, I hope that output and new_path are actually strings (I assume so, but it's not clear from what you posted). But assuming all of that is sorted out:

sorting = Popen(...)
sorting_output = sorting.communicate()[0]

That should store the contents of the subprocess's standard output to sorting_output.

Comments

0

To emulate the shell command:

$ sort -k2  /homes/varshith/maf > /homes/varshith/maf_sort.bed

i.e., to sort /homes/varshith/maf file by the 2nd column and to store the sorted output to /homes/varshith/maf_sort.bed file in Python:

#!/usr/bin/env python
from subprocess import check_call

field = '-k2'
input_path  = '/homes/varshith/maf'
output_path = input_path + '_sort.bed'
with open(output_path, 'wb', 0) as output_file:
    check_call(["sort", field, input_path], stdout=output_file)

It overwrites the output file. To append to the file instead, use ab mode instead of wb.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.