How to use the mv command in Python with subprocess

后端 未结 4 2115
天命终不由人
天命终不由人 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:30

    Here's a simple way to work with subprocess Popen

    import subprocess
    import os
    
    class FolderCommands:
        src = None
        dst = None
    
        def __init__(self, src, dst):
            self.src = src
            self.dst = dst
    
        def move(self):
            listOfFiles = os.listdir(self.src)
            print(listOfFiles)
            modify_src = self.src.replace(" ", "\ ")
            dst = self.dst.replace(" ", "\ ")
            for f in listOfFiles:
                #Attaching the filename at the end of the src path
    
                fullPath = modify_src + "/'" + f +"'"
                subprocess.Popen("mv" + " " + fullPath + " " + dst, shell=True)
    
    obj = FolderCommands(input("Enter Source path"), input("Enter Destination path"))
    obj.move()
    

提交回复
热议问题