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.
os.listdir()and filter this list using regex. The pass the matching elements to your subprocess in a loop.cp. But the default behavior forsubprocessis to not involve a shell at all - you'd need to addshell=Trueto get this to work (in which case you should supply the command as a single string, rather than a list of individual parameters).