使用Python子进程处理带有括号的文件路径 [英] Processing file paths with parentheses with Python subprocess

查看:118
本文介绍了使用Python子进程处理带有括号的文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要处理的文件的路径中包含括号.

The paths of the files I want to process contain parentheses in them.

path = "/dir/file (with parentheses).txt"

我正在尝试按以下方式在Python中对其进行处理:

I'm trying to process them in Python as follows:

subprocess.call("./process %s" % path, shell=True)

但是,出现以下错误

/bin/sh: 1: Syntax error: "(" unexpected

如何传递正确的字符串以处理正确的路径?

How can I pass the correct string to process the proper path?

推荐答案

尝试一下

subprocess.call('./process "%s"' % path, shell=True)

我想问题更多是文件名中的空格.其中带有空格的文件名应使用引号引起来,例如 ./process"foo bar.txt" 或转义,例如 ./process foo \ bar.txt .

I guess problem is more with space in file name. File names with spaces in them should be enclosed in quotes like this ./process "foo bar.txt" or escaped like this ./process foo\ bar.txt.

这篇关于使用Python子进程处理带有括号的文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆