简体   繁体   English

如何在 bash 命令中传递变量?

[英]How to pass variable in bash command?

I am trying to convert multiple.doc files to.docx format using antiword.我正在尝试使用 antiword 将多个 .doc 文件转换为 .docx 格式。 How to write a command like this:如何编写这样的命令:

for file in os.listdir(directory):
    subprocess.run("antiword file > file+'.docx'")

it shows this error:它显示了这个错误:

[Errno 2] No such file or directory: "antiword file > file+'.docx'": "antiword file > file+'.docx'"

There is no other way to read.doc file properly that worked for me other than antiword.除了 antiword 之外,没有其他方法可以正确读取对我有用的 read.doc 文件。

Subprocess doesn't run bash.子进程不运行 bash。 So run bash and pass arguments with arguments remembering about proper quoting.所以运行 bash 并通过 arguments 和 arguments 记住正确的引用。

subprocess.run(["bash", "-c", "antiword \"$1\" > \"$1\".docx", "_", file])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM