954,546 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to copy a file

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

pinder
Newbie Poster
5 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

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)]
solsteel
Junior Poster
145 posts since Mar 2007
Reputation Points: 86
Solved Threads: 42
 

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")
ghostdog74
Junior Poster
156 posts since Apr 2006
Reputation Points: 75
Solved Threads: 44
 

thx alot for ur help guys

pinder
Newbie Poster
5 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

print 1 to 10 numbers using c

nrupparikh
Newbie Poster
5 posts since Dec 2008
Reputation Points: 7
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You