How do i copy files with shutil in python -


im trying copy .txt file 1 dest another. code running file isnt copying. doing wrong?

import shutil import os src = "/c:/users/mick/temp" src2 = "c:/users/mick" dst = "/c:/users/mick/newfolder1/"  files in src , src2:     if files.endswith(".txt"):         shutil.copy(files, dst) 

your loop isn't searching through files of each of sources. in addition loop isn't looping through each of sources rather letters in src , src2 present in both. adjustment should handle need.

import os src = ["c:/users/mick/temp", "c:/users/mick"] dst = "c:/users/mick/newfolder1/"  source in src:     src_file in os.listdir(source):         if src_file.endswith(".txt"):             old_path = os.path.join(source, src_file)             new_path = os.path.join(dst, src_file)             os.rename(old_path, new_path) 

you shouldn't need shutil situation more powerful os.rename attempts handle different scenarios little better (to knowledge). if "newfolder1" isn't existant want replace os.rename() os.renames() attempts create directories inbetween.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

jquery - Keeping Kendo Datepicker in min/max range -