user2269123
user2269123

Reputation: 15

Copying files based on File name rather than data type in Python

I am very new to Python. I want to copy set of files from one folder to another based on the name and not based on data types. So I have a source folder with files like Hello.txt, Hello.dat, Hello.pdf, World.txt, World.dat, Word.pdf. Now I just want to copy files beginning with "Hello" into another folder. I tried many ways but all seems to be based on data type.

Upvotes: 1

Views: 3755

Answers (1)

Rakesh
Rakesh

Reputation: 82755

import os
import shutil
files = os.listdir("Path")
for file in files:
    if file.startswith("Hello"):
       shutil.copy("Full path to file", "Full path to dest folder")

Upvotes: 2

Related Questions