0

I am trying to automate a particular process using subprocess module in python. For example, if I have a set of files that start with a word plot and then 8 digits of numbers. I want to copy them using the subprocess run command.

copyfiles = subprocess.run(['cp', '-r', 'plot*', 'dest'])

When I run the code, the above code returns an error "cp: plot: No such file or directory*"

How can I execute such commands using subprocess module? If I give a full filename, the above code works without any errors.

4
  • I'd create a list of candidates first using os.listdir() and filter this list using regex. The pass the matching elements to your subprocess in a loop. Commented Sep 16, 2022 at 13:37
  • 2
    Wildcards are processed by the shell, not by individual utilities such as cp. But the default behavior for subprocess is to not involve a shell at all - you'd need to add shell=True to get this to work (in which case you should supply the command as a single string, rather than a list of individual parameters). Commented Sep 16, 2022 at 14:03
  • @jasonharper please make an answer out of this comment. You are exactly right Commented Sep 16, 2022 at 14:17
  • @white I did this, and it works for me. Thanks for the quick solution. Commented Sep 17, 2022 at 15:54

1 Answer 1

0

I have found a useful but probably not the best efficent code fragment from this post, where an additional python library is used (shlex), and what I propose is to use os.listdir method to iterate over folder that you need to copy files, save in a list file_list and filter using a lambda function to extract specific file names, define a command sentence as string and use subproccess.Popen() to execute the child process to copy the files on to a destination folder.

import shlex
import os
import subprocess

# chage directory where your files are located at
os.chdir('C:/example_folder/')

# you can use os.getcwd function to check the current working directory
print(os.getcwd)
 
# extract file names
file_list = os.listdir() 
file_list = list(filter(lambda file_name: file_name.startswith('plot'), file_list))

# command sentence
cmd = 'find test_folder -iname %s -exec cp {} dest_folder ;'

for file in file_list:
    subprocess.Popen(shlex.split(cmd % file)) 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for resolving the issue. I just made a list of filenames and used a loop to copy the files to the destination folder.

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.