Moving all files from one directory to another using Python

前端 未结 9 1762
长发绾君心
长发绾君心 2020-12-24 00:57

I want to move all text files from one folder to another folder using Python. I found this code:

import os, shutil, glob

dst = \'/path/to/dir/Caches/com.app         


        
9条回答
  •  抹茶落季
    2020-12-24 01:22

    Please, take a look at implementation of the copytree function which:

    • List directory files with:

      names = os.listdir(src)

    • Copy files with:

    for name in names:
      srcname = os.path.join(src, name)
      dstname = os.path.join(dst, name)
      copy2(srcname, dstname)
    

    Getting dstname is not necessary, because if destination parameter specifies a directory, the file will be copied into dst using the base filename from srcname.

    Replace copy2 by move.

提交回复
热议问题