April 2017
Beginner to intermediate
312 pages
7h 23m
English
The shutil module (https://docs.python.org/3/library/shutil.html) enables moving and copying files between folders using the move() and copy() methods. In the previous section, we listed all text files within the folder, txt_files. Let's move these files to the current directory (where the code is being executed) using move(), make a copy of these files once again in txt_files and finally remove the text files from the current directory:
import globimport shutilimport osif __name__ == "__main__": # move files to the current directory for file in glob.glob('txt_files/file1[0-9][0-9].txt'): shutil.move(file, '.') # make a copy of files in the folder 'txt_files' and delete them for file in glob.glob('file1[0-9][0-9].txt'): ...Read now
Unlock full access