Search code examples
pythonbashprocess-substitution

feed uncompressed file to command line argument


Let's say I have a gzipped file, but my script only takes in an uncompressed file.

Without modifying the script to take in a compressed file, could I uncompress the file on the fly with bash?

For example:

python ../scripts/myscript.py --in (gunzip compressed_file.txt.gz)

Solution

  • You can use a process substitution, as long as the Python script doesn't try to seek backwards in the file:

    python ../scripts/myscript.py --in <(gunzip compressed_file.txt.gz)
    

    Python receives a file name as an argument; the name just doesn't refer to a simple file on disk. It can only be opened in read-only mode, and attempts to use the seek method will fail.


    If you were using zsh instead of bash, you could use

    python ../scripts/myscript.py --in =(gunzip compressed_file.txt.gz)
    

    and Python would receive the name of an actual (temporary) file that could be used like any other file. Said file would be deleted by the shell after python exits, though.