简体   繁体   English

如何复制具有特定扩展名的文件夹目录中的文件,然后将其保存到新文件夹中而不覆盖已复制的文件?

[英]How do I copy files in a folder directory with a specific extension and save it to a new folder with out overwriting copied files?

Here is the code I am working on: 这是我正在处理的代码:

from qgis.core import*
import glob, os, shutil, time, qgis

path = r"C:\Temp\testinput"
dest = r"C:\Temp\testoutput"

fname = []
for root,d_names,f_names in os.walk(path):
    for f in f_names:
        if f.endswith('.kml'):
            src = os.path.join(root,f)
            print(time.strftime('%m/%d/%Y', time.gmtime(os.path.getmtime(src))))
            print(os.path.realpath(src))
            shutil.copy2(src, dest)

this code transverses through the directory and copies the files but does overwrites files with the same name. 此代码遍历目录并复制文件,但确实覆盖了具有相同名称的文件。 How do I prevent the overwriting part? 如何防止覆盖部分? I would like to have the same type of filed renamed to "filename-copy" if it catches a file with the same name in the new folder. 我想将相同类型的文件名重命名为“ filename-copy”,如果它在新文件夹中捕获了一个具有相同名称的文件。

Very quick answer; 很快的答案;

If the extension is known, it is not that hard. 如果知道扩展名,那就不难了。 You can check upfront if a file exists. 您可以预先检查文件是否存在。 By adding something like this 通过添加这样的东西

exists = os.path.isfile(dest)
if exists:
    os.rename(dest , dest.replace('.kml', '-copy.kml'))

So the entire thing looks like this: 所以整个事情看起来像这样:

from qgis.core import*
import glob, os, shutil, time, qgis

path = r"C:\Temp\testinput"
dest = r"C:\Temp\testoutput"

fname = []
for root,d_names,f_names in os.walk(path):
    for f in f_names:
        if f.endswith('.kml'):
            src = os.path.join(root,f)
            print(time.strftime('%m/%d/%Y', time.gmtime(os.path.getmtime(src))))
            print(os.path.realpath(src))
            exists = os.path.isfile(dest)
            if exists:
                os.rename(dest , dest.replace('.kml', '-copy.kml'))
            shutil.copy2(src, dest)

Not so very quick answer; 回答不是那么迅速。

But, this assumes that the "file-copy.kml" does not already exist. 但是,这假定“ file-copy.kml”尚不存在。 If you want to keep an X amount of copies, perhaps rename them differently. 如果要保留X份副本,则可以使用其他名称重命名。 In that case, I would advice something like this: 在这种情况下,我建议如下:

    exists = os.path.isfile(dest)
    if exists:
        os.rename(dest , dest.replace('.kml', '-copy_'+id_generator()+'.kml'))

Where you place this someplace at the top of your file. 将此位置放置在文件顶部的位置。

import string
import random
def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
    return ''.join(random.choice(chars) for _ in range(size))

Where I have borrowed the function that generates some random characters from this question; 我在哪里借用了从该问题生成一些随机字符的函数; Random string generation with upper case letters and digits in Python 在Python中使用大写字母和数字生成随机字符串

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查复制到特定文件夹的新文件? - How to check for new files that are copied to a specific folder? 如何将具有特定文件扩展名的文件复制到我的python(2.5版)脚本中的文件夹中? - How do I copy files with specific file extension to a folder in my python (version 2.5) script? 在SVN中的目录中搜索具有特定文件扩展名的文件,然后复制到另一个文件夹? - Search directory in SVN for files with specific file extension and copy to another folder? 在目录中创建一个文件夹并将文件保存在新文件夹中 - create a folder in the directory and save files in the new folder 我如何在 python 的特定文件夹中保存.json 文件 - How do i save .json files in specific folder in python 如何将一种特定类型的所有文件从一个文件夹复制到Python中的另一个文件夹 - How do I copy all files of one specific type from a folder to another folder in Python 尝试将具有特定扩展名的文件从源文件夹复制到目标文件夹 - Trying to copy files with a specific extension from source folder to destination folder 如何更改目录和文件夹的所有文件的扩展名 - how to change extension of all the files of a directory and folder 递归复制文件夹并更改复制文件的文件夹/文件名 - Recursively copy a folder and change folder/file names of copied files 如何使用 os 将文件保存到新文件夹? - How to save files to new folder using os?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM