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.

  • 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. – white Sep 16 '22 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). – jasonharper Sep 16 '22 at 14:03
  • @jasonharper please make an answer out of this comment. You are exactly right – Matteo Zanoni Sep 16 '22 at 14:17
  • @white I did this, and it works for me. Thanks for the quick solution. – Vinoth Paramanantham Sep 17 '22 at 15:54

1 Answers1

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)) 
user11717481
  • 1
  • 9
  • 15
  • 25