1

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 (\u005C), but it also returns double "\". What should I do in this case?

2
  • 2
    the subprocess module receives the command as a list of parts, not as a strings string in a list. You should try: subprocess.run(['copy', '/b', r'D:\deneme\1\*.ts', r'D:\deneme\1\1.ts']). I'm not a Windows user, but copy might need shell=True to be set. And BTW, there are direct options to copy something in Python. Commented Aug 7, 2019 at 16:50
  • 1
    shell=True did the job, thank you. If you add it as answer, I could flag it. Commented Aug 7, 2019 at 17:07

1 Answer 1

1

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"])
Sign up to request clarification or add additional context in comments.

1 Comment

Tried both, neither worked. And to make sure the directories are correct, I typed "copy /b D:\deneme\1*.ts D:\deneme\1\1.ts" into shell and it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.