I am trying to rename a set of pdf files in my desktop using a simple python script. I am somehow not very successful. My current code is :
import os,subprocess
path = "/Users/armed/Desktop/"
for file in os.listdir(path)
command = ['mv', '*.pdf' , 'test.pdf'] // mv Command to rename files to test.pdf
subprocess.call(command)
The output i get for this code is 1 and the files are not renamed. The same command works when executed in the terminal. I am using a Mac (if that helps in any way)
Python is not going to expand the shell wildcard in the string by default. You can also do this without a subprocess. But your code will lose all pdf files except the last one.
But I'm sure that's not what you really want. You'll need a better destination name.
Except it's not the same command. The code is running:
but when you type it out it runs:
The difference is that the shell globs the
*
wildcard before executingmv
. You can simulate what it does by using theglob
module.