Hi,
I'm trying to copy a file in python, I have imported the shutil module
My problem is that I want to copy file in a directory beginning with a certain name. Example I have 5 file in the fodler but i want to copy all the one tat begin with journal.something How could i do that

thanks

Recommended Answers

All 4 Replies

Create a list of file names that match your criteria:

import os

path_name = 'your_directory'
key = 'journal'

nameList = [os.path.join(path_name, n) for n in os.listdir(path_name) \
            if os.path.isfile(os.path.join(path_name, n)) and n.startswith(key)]

Hi,
I'm trying to copy a file in python, I have imported the shutil module
My problem is that I want to copy file in a directory beginning with a certain name. Example I have 5 file in the fodler but i want to copy all the one tat begin with journal.something How could i do that

thanks

import shutil,glob,os
os.chdir("wherefileis")
for FILE in glob.glob("journal*"):
     shutil.copy(FILE, "someotherdir")

thx alot for ur help guys

print 1 to 10 numbers using c

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.