Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm trying to use Python's package subprocess to gunzip and gzip files. I'm using the following command:

subprocess.call(['gunzip', a_path+myfile_gz], shell=True)

Then I get the error message:

gzip: compressed data not read from a terminal. Use -f to force decompression.

So I typed:

subprocess.call(['gunzip', '-f', a_path+myfile_gz], shell=True)

And I get exactly the same error message. If I type shell=False, it's worse.

It's really weird because if I do it from my terminal, it works fine! Does anybody knows what's wrong?

share|improve this question
    
Show us the complete call, like " ".join(['gunzip', a_path+myfile_gz]) –  scai Sep 4 '12 at 6:47
    
Why don't you use the gzip package? It's in the standard library. –  daniel kullmann Sep 4 '12 at 8:18
1  
What errors do you get when you use shell=False? It works for me when I do that. –  daniel kullmann Sep 4 '12 at 8:21
add comment

1 Answer

The problem is that you're passing shell=True, so you're executing

sh -c gunzip /path/to/file.gz

(assuming a_path+myfile_gz is "/path/to/file.gz"). In other words, because the shell parameter is True, the first element of the command list is a shell snippet; the other arguments are arguments to that shell snippet (here just $0). This is explained in the documentation.

If you leave out shell=True, you'll get working but not good code. Don't manipulate paths as strings; use the functions in the os.path. That a_path+myfile_gz should probably be os.path.join(a_path, myfile_gz) instead.

In any case, Python has built-in support for gzip. Don't use an external command.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.