1

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.

2
  • 3
    Please show us the code you have so far. Commented Apr 11, 2013 at 7:03
  • What ways? The obvious detections of "data type" that I can think of are, in fact, "based on file name" - they're simply looking at the end of the file name instead of the beginning. If you have "tried ways" then they shouldn't "seem" to be based on something; you should know. Please don't just use code that you find without first attempting to understand it. Commented Apr 11, 2013 at 7:09

1 Answer 1

2
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")
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Rakesh thanks. I used the following command to move the files using the terminal import os import shutil import glob os.system('mv Hello.* path_to_Destination_folder')
Thanks a lot Rakesh .... I implemented it and it worked very well. Your help is highly appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.