简体   繁体   English

从 Python 中将文件名传递给子进程

[英]Passing filename to a subprocess from within Python

I'm having problems calling an external command from within a python routine.我在从 python 例程中调用外部命令时遇到问题。 I've written code before that calls simple commands but this one is a bit trickier and I'm just floundering around and getting nowhere.在调用简单命令之前我已经编写过代码,但是这个有点棘手,我只是在四处挣扎,无处可去。

The command is one of the GMT (Generic Mapping Tools) commands - "gmtselect" which takes a file of latitude/longitude coordinates and sees which ones are within a polygon defined by a series of lat/long points in a separate file.该命令是 GMT(通用映射工具)命令之一 - “gmtselect”,它获取纬度/经度坐标文件,并查看哪些位于由单独文件中的一系列纬度/经度点定义的多边形内。

If we call these two file "points.txt" and "polygon.txt" then from the command line the call would be:如果我们将这两个文件称为“points.txt”和“polygon.txt”,那么从命令行调用将是:

gmtselect <polygon.txt -Fpolygon.txt 

I've tried various ways but cannot see how to do this using subprocess.Popen我尝试了各种方法,但看不到如何使用 subprocess.Popen 做到这一点

Any informed suggestions?有什么明智的建议吗?

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