简体   繁体   中英

How Can I Send a “Copy” Command Using Python Subprocess

I am trying to send a command prompt command using Python Subprocess. I haven't been able to get my code to send "\\" in the command. I usually specify directions as:

"D:\\deneme\\1"

or

r"D\deneme\\1"

So, I tried both as:

import subprocess
subprocess.run(["copy /b D:\\deneme\\1\\*.ts D:\\deneme\\1\\1.ts"])

But the string is sent as it is so I get the error, "FileNotFoundError: [WinError 2] The system cannot find the file specified". I also tried using the unicode number (\\), but it also returns double "\\". What should I do in this case?

Your system shell is try to find a command with the name copy /b D:\\\\... , not run copy with 3 arguments. Either drop the list:

subprocess.run("copy /b D:\\deneme\\1\\*.ts D:\\deneme\\1\\1.ts")

or pass a proper list containing the command name and its arguments as separate elements.

subprocess.run(["copy", "/b", "D:\\deneme\\1\\*.ts", "D:\\deneme\\1\\1.ts"])

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