简体   繁体   中英

How to pass variable in bash command?

I am trying to convert multiple.doc files to.docx format using antiword. 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.

Subprocess doesn't run bash. So run bash and pass arguments with arguments remembering about proper quoting.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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