How to use the mv command in Python with subprocess

后端 未结 4 2114
天命终不由人
天命终不由人 2021-02-19 10:39

I have a lot of files in /home/somedir/subdir/ and I\'m trying to move them all up to /home/somedir programmatically.

right now I have this:

subprocess.c         


        
4条回答
  •  孤街浪徒
    2021-02-19 11:22

    You can solve this by adding the parameter shell=True, to take into account wildcards in your case (and so write the command directly, without any list):

    subprocess.call("mv /home/somedir/subdir/* somedir/", shell=True)
    

    Without it, the argument is directly given to the mv command with the asterisk. It's the shell job to return every files which match the pattern in general.

提交回复
热议问题