【问题标题】:Copy files from one folder to another and rename the files将文件从一个文件夹复制到另一个文件夹并重命名文件
【发布时间】:2018-12-03 11:31:17
【问题描述】:

我想将大量 .html 文件复制到另一个文件夹。另外,我想将文件名更改为“FoldersName_Filename”。

import shutil
    import os
    for Jahr in range(2000,2014):
        for Datei in os.listdir("S:\\DA\\html\\Jahrescluster\\%i" %Jahr):
            shutil.copy2(src="S:\\html\\Jahrescluster\\%i\\%s" % (Jahr, Datei), dst="S:\\html\\2000-2013\\%i_%s" (Jahr, Datei))

代码产生以下错误:

shutil.copy2(src="S:\\html\\Jahrescluster\\%i\\%s" % (Jahr, Datei), dst="S:\\html\\2000-2013\\%i_%s" (Jahr, Datei))

TypeError: 'str' object is not callable

【问题讨论】:

    标签: python file copy shutil


    【解决方案1】:

    使用str.format

    例如:

    import shutil
    import os
    
    for Jahr in range(2000,2014):
        for Datei in os.listdir("S:\\DA\\html\\Jahrescluster\\%i" %Jahr):
            shutil.copy2(src="S:\\html\\Jahrescluster\\{}\\{}".format(Jahr, Datei), dst="S:\\html\\2000-2013\\{}_{}".format(Jahr, Datei))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-22
      • 2013-11-30
      • 1970-01-01
      • 2023-01-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多