Moving all files from one directory to another using Python

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

    suprised this doesn't have an answer using pathilib which was introduced in python 3.4+

    additionally, shutil updated in python 3.6 to accept a pathlib object more details in this PEP-0519

    Pathlib

    from pathlib import Path
    
    src_path = '\tmp\files_to_move'
    
    for each_file in Path(src_path).glob('*.*'): # grabs all files
        trg_path = each_file.parent.parent # gets the parent of the folder 
        each_file.rename(trg_path.joinpath(each_file.name)) # moves to parent folder.
    

    Pathlib & shutil to copy files.

    from pathlib import Path
    import shutil
    
    src_path = '\tmp\files_to_move'
    trg_path = '\tmp'
    
    for src_file in Path(src_path).glob('*.*'):
        shutil.copy(src_file, trg_path)
    
    0 讨论(0)
  • 2020-12-24 01:15
    def copy_myfile_dirOne_to_dirSec(src, dest, ext): 
    
        if not os.path.exists(dest):    # if dest dir is not there then we create here
            os.makedirs(dest);
            
        for item in os.listdir(src):
            if item.endswith(ext):
                s = os.path.join(src, item);
                fd = open(s, 'r');
                data = fd.read();
                fd.close();
                
                fname = str(item); #just taking file name to make this name file is destination dir     
                
                d = os.path.join(dest, fname);
                fd = open(d, 'w');
                fd.write(data);
                fd.close();
        
        print("Files are copyed successfully")
    
    0 讨论(0)
  • 2020-12-24 01:17

    Try this:

    import shutil
    import os
        
    source_dir = '/path/to/source_folder'
    target_dir = '/path/to/dest_folder'
        
    file_names = os.listdir(source_dir)
        
    for file_name in file_names:
        shutil.move(os.path.join(source_dir, file_name), target_dir)
    
    0 讨论(0)
  • 2020-12-24 01:19
    import shutil 
    import os 
    import logging
    
    source = '/var/spools/asterisk/monitor' 
    dest1 = '/tmp/'
    
    
    files = os.listdir(source)
    
    for f in files:
            shutil.move(source+f, dest1)
    
    logging.basicConfig(filename='app.log', filemode='w', format='%(name)s
    - %(levelname)s - %(message)s')
    
    logging.info('directories moved')
    

    A little bit cooked code with log feature. You can also configure this to run at some period of time using crontab.

    * */1 * * * python /home/yourprogram.py > /dev/null 2>&1
    

    runs every hour! cheers

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-24 01:23

    For example, if I wanted to move all .txt files from one location to another ( on a Windows OS for instance ) I would do it something like this:

    import shutil
    import os,glob
    
    inpath = 'R:/demo/in' 
    outpath = 'R:/demo/out'
    
    os.chdir(inpath)
    for file in glob.glob("*.txt"):
    
        shutil.move(inpath+'/'+file,outpath)
    
    0 讨论(0)
提交回复
热议问题