簡體   English   中英

從 Python 中將文件名傳遞給子進程

[英]Passing filename to a subprocess from within Python

我在從 python 例程中調用外部命令時遇到問題。 在調用簡單命令之前我已經編寫過代碼,但是這個有點棘手,我只是在四處掙扎,無處可去。

該命令是 GMT(通用映射工具)命令之一 - “gmtselect”,它獲取緯度/經度坐標文件,並查看哪些位於由單獨文件中的一系列緯度/經度點定義的多邊形內。

如果我們將這兩個文件稱為“points.txt”和“polygon.txt”,那么從命令行調用將是:

gmtselect <polygon.txt -Fpolygon.txt 

我嘗試了各種方法,但看不到如何使用 subprocess.Popen 做到這一點

有什么明智的建議嗎?

import subprocess

with open('filename.txt') as f:
    subprocess.run(['gmtselect', '-Fother.txt'], stdin=f)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM