Python moving files and directories from one folder to another

前端 未结 4 1676
别跟我提以往
别跟我提以往 2021-01-06 11:22

I would like to move in python files and directories in one directory to another directory with overwrite ability.

I started with the following code:



        
4条回答
  •  天命终不由人
    2021-01-06 12:03

    (Python 3.6) From a previous answer (can't add comment)

    I think that the line

    if not os.path.isdir(node):
    

    Should read

    if not os.path.isdir(os.path.join(source, node))
    

    Otherwise it will always return True and move the sub folders as well.

    >>> import os
    >>> import shutil
    
    >>> for node in os.listdir(path):
    ...      if not os.path.isdir(os.path.join(path, node)):
    ...          shutil.move(os.path.join(path, node) , os.path.join(compteurfolder, node))
    

提交回复
热议问题