Moving all files from one directory to another using Python

前端 未结 9 1744
长发绾君心
长发绾君心 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:40

    Copying the ".txt" file from one folder to another is very simple and question contains the logic. Only missing part is substituting with right information as below:

    import os, shutil, glob
    
    src_fldr = r"Source Folder/Directory path"; ## Edit this
    
    dst_fldr = "Destiantion Folder/Directory path"; ## Edit this
    
    try:
      os.makedirs(dst_fldr); ## it creates the destination folder
    except:
      print "Folder already exist or some error";
    

    below lines of code will copy the file with *.txt extension files from src_fldr to dst_fldr

    for txt_file in glob.glob(src_fldr+"\\*.txt"):
        shutil.copy2(txt_file, dst_fldr);
    

提交回复
热议问题