Unable to use xopen handle as output of subprocess (`io.UnsupportedOperation: fileno`)
My goal is to run a shell command and stream the output to a compressed file. I figured xopen would be a good way to achieve this, however I can't get it to work. Here is a minimal reproducible example:
```py
from xopen import xopen
from subprocess import run
with xopen("file1.txt", "w") as f:
f.write("something")
with xopen("file2.txt.xz", "wb") as f:
run(["cat", "file1.txt"], stdout=f)
# also tried:
# • Popen(["cat", "file1.txt"], stdout=f)
# • call(["cat", "file1.txt"], stdout=f)
# • output file extension of .gz, .zst
```
Error:
```
File "test.py", line 8, in <module>
run(["cat", "file1.txt"], stdout=f)
File "…/python3.10/subprocess.py", line 503, in run
with Popen(*popenargs, **kwargs) as process:
File "…/python3.10/subprocess.py", line 837, in __init__
errread, errwrite) = self._get_handles(stdin, stdout, stderr)
File "…/python3.10/subprocess.py", line 1638, in _get_handles
c2pwrite = stdout.fileno()
io.UnsupportedOperation: fileno
```
An alternative is to use `check_output`, but I would like to do this without storing the subprocess output in memory since it may be large.
```py
# This works but output is stored in memory
output = check_output(["cat", "file1.txt"])
with xopen("file2.txt.xz", "wb") as f:
f.write(output)
```
Questions:
1. Is there a way to achieve my goal with other subprocess functions/parameters?
2. If not, are there plans to support this in xopen?
关闭于 2024-07-26 4 条评论