pandas 使用 Python 复制和重命名 Excel 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34357983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:24:15  来源:igfitidea点击:

Copying and renaming excel files with Python

pythonexcelpandas

提问by Horv

I am trying to copy an excel file and rename it. For example I have a file HI.xlsxand 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.xlsxis created, but it is corrupted.

我正在尝试复制一个 excel 文件并重命名它。例如,我有一个文件HI.xlsx,我想复制它并将其重命名为BYE.xlsx.
我执行此操作的代码如下,当我运行它时,文件BYE.xlsx已创建,但已损坏。

Here is what I am using:

这是我正在使用的:

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

回答by DeepSpace

You should be using shutil.copy():

你应该使用shutil.copy()

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