如何使用Python在目录中重新保存excel文件?

2024-09-19 23:35:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我在给定的目录(C:\ucustom\myfiles)中有一堆Excel文件(.xls)。我想以编程方式使用pythonv3.6在同一个目录中重新保存这些文件。我该怎么做?(无需打开每个文件,只需将每个.xls文件重新保存到位)。谢谢!你知道吗


Tags: 文件目录编程方式xlsexcelmyfilesucustom
1条回答
网友
1楼 · 发布于 2024-09-19 23:35:01

下面的代码将重新保存目录中的所有xls文件(例如,1.xls将另存为COPY OF 1.xls)。你知道吗

import os
from shutil import copyfile

directory = "C:\\ucustom\\myfiles"

for filename in os.listdir(directory):
    if filename.endswith(".xls"):
        copyfile(os.path.join(directory, filename),
            os.path.join(directory, "COPY OF " + filename))

相关问题 更多 >