我无法使用pdal
从Python运行subprocess
bash命令。
基于Running Bash commands in Python:
import os, subprocess
input = '/path/to/file.ply'
output = '/path/to/statfile.json'
if not os.path.isfile(output):
open(output, 'a').close()
bashcmd = ("pdal info --boundary "
+input
+" > "
+output
)
print("Bash command is:\n{}\n".format(bashcmd))
process = subprocess.Popen(bashcommand.split(),
stdout=subprocess.PIPE,
shell=True)
output, error = process.communicate()
print("Output:\n{}\n".format(output))
print("Error:\n{}\n".format(error))
哪个在Python控制台中给了我以下输出:
Bash command is:
pdal info --boundary /path/to/file.ply > /path/to/statfile.json
Output:
Usage:
pdal <options>
pdal <command> <command options>
--command The PDAL command
--debug Sets the output level to 3 (option deprecated)
--verbose, -v Sets the output level (0-8)
--drivers List available drivers
--help, -h Display help text
--list-commands List available commands
--version Show program version
--options Show options for specified driver (or 'all')
--log Log filename (accepts stderr, stdout, stdlog, devnull as
special cases)
--logtiming Turn on timing for log messages
The following commands are available:
- delta
- diff
- fauxplugin
- ground
- hausdorff
- info
- merge
- pcl
- pipeline
- random
- smooth
- sort
- split
- tindex
- translate
See http://pdal.io/apps/ for more detail
Error:
None
似乎只有在调用“ pdal
”之后,它才停止读取命令的参数,这会打印此帮助消息。
如果我复制第一张照片的输出并将其粘贴到bash终端中,它将正常工作,并为我提供具有所需元数据的输出文件。但是从Python不会创建任何输出文件。
我想知道为什么(例如,重定向是否有问题,或者计算本身通常可能需要20秒左右的时间?),以及如何从Python执行此命令?
答案 0 :(得分:1)
这里有多个错误。
bashCommand
,而不是您在bashcmd
上方定义的变量。stderr
。 (我会模糊地假设您仍然不需要标准错误。)split()
运行该命令,则不应shell=True
。更广泛地讲,您应该避免使用shell=True
并让Python通过将输出连接到您打开的文件来为您处理重定向。并且在现代,如果可以使用subprocess.Popen()
或subprocess.run()
或朋友,则实际上不应该使用subprocess.check_call()
。
import subprocess
input = '/path/to/file.ply'
output = '/path/to/statfile.json'
with open(output, 'a') as handle:
bashcmd = ['pdal', 'info', '--boundary', input]
#print("Bash command is:\n{}\n".format(bashcmd))
result = subprocess.run(bashcmd, stdout=handle, stderr=subprocess.PIPE)
# No can do because output goes straight to the file now
##print("Output:\n{}\n".format(output))
#print("Error:\n{}\n".format(result.stdout))