Using regular expression in subprocess module

Viewed 30

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.

1 Answers

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)) 
Related