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.
-
3Please show us the code you have so far.NPE– NPE2013-04-11 07:03:11 +00:00Commented 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.Karl Knechtel– Karl Knechtel2013-04-11 07:09:09 +00:00Commented Apr 11, 2013 at 7:09
Add a comment
|
1 Answer
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")
2 Comments
user2269123
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')
user2269123
Thanks a lot Rakesh .... I implemented it and it worked very well. Your help is highly appreciated.