Monday, 14 May 2018

Copying and renaming excel files with Python




I am trying to copy an excel file and rename it. For example I have a file HI.xlsx and I want to copy it and rename it as BYE.xlsx.
My code to do this is below and when I run it, the file BYE.xlsx is created, but it is corrupted.



Here is what I am using:



fIn = open(r"HI.xlsx")
fOut = open(r"BYE.xlsx", "w")

Answer



You should be using shutil.copy():




shutil.copy("HI.xlsx", "BYE.xlsx")

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...