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

copying a file in python

Hai friends
I have a small problem with my code...
I want to copy an existing file (its a model MS Excel file) to a new file & name it with the current date...
First I tried to create the model Excel file, but when I opened it, it had only one sheet inside it, so I tried to copy the existing file, but I couldnt find any bright ideas... can anyone show me a way?


import sys
import win32com.client

fileVM = open('e:/disk/vm.xls','a')
fileVM.close()

The above code creates the file with a single Excel Sheet inside it. But my model file has a lot of info, so I wud prefer to copy & then rename the existing file...

xav.vijay
Junior Poster in Training
55 posts since Aug 2005
Reputation Points: 10
Solved Threads: 6
 

are you just looking for a was to copy a file(I might be missing something)

import shutil
shutil.copy2('e:/disk/vm.xls', 'new_name_of_copy')


I have only tested this on my linux system, but it should also work on a windows platform.

shanenin
Posting Whiz in Training
217 posts since May 2005
Reputation Points: 10
Solved Threads: 17
 

You may want to use shutil.copy(sourcefile, destinationfile), it gives copy the current date, shutil.copy2(sourcefile, destinationfile) gives the date of the file you copy.

bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184
 
You may want to use shutil.copy(sourcefile, destinationfile), it gives copy the current date, shutil.copy2(sourcefile, destinationfile) gives the date of the file you copy.

good point, I was not sure on the exact difference

shanenin
Posting Whiz in Training
217 posts since May 2005
Reputation Points: 10
Solved Threads: 17
 

Thanks guys , its working fine with windows as well...

xav.vijay
Junior Poster in Training
55 posts since Aug 2005
Reputation Points: 10
Solved Threads: 6
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You