久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如果文件名已經存在,Python 將文件復制到新目錄

Python copy files to a new directory and rename if file name already exists(如果文件名已經存在,Python 將文件復制到新目錄并重命名)
本文介紹了如果文件名已經存在,Python 將文件復制到新目錄并重命名的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我已經閱讀了 這個帖子 但是當我將它實現到我的代碼中時,它只適用于幾次迭代.

I've already read this thread but when I implement it into my code it only works for a few iterations.

我正在使用 python 遍歷一個目錄(我們稱之為移動目錄),主要將 pdf 文件(匹配唯一 ID)復制到另一個目錄(基本目錄)到匹配文件夾(具有相應的唯一 ID).我開始使用 shutil.copy 但如果有重復,它會覆蓋現有文件.

I'm using python to iterate through a directory (lets call it move directory) to copy mainly pdf files (matching a unique ID) to another directory (base directory) to the matching folder (with the corresponding unique ID). I started using shutil.copy but if there are duplicates it overwrites the existing file.

我希望能夠搜索相應的文件夾以查看該文件是否已存在,如果出現多個,則對其進行迭代命名.

I'd like to be able to search the corresponding folder to see if the file already exists, and iteratively name it if more than one occurs.

例如

  • 將文件 1234.pdf 復制到基本目錄 1234 的文件夾中.
  • 如果存在 1234.pdf,則將其命名為 1234_1.pdf,
  • 如果另一個 pdf 被復制為 1234.pdf,那么它將是 1234_2.pdf.

這是我的代碼:

import arcpy
import os
import re
import sys
import traceback
import collections
import shutil

movdir = r"C:Scans"
basedir = r"C:Links"

try:
    #Walk through all files in the directory that contains the files to copy
    for root, dirs, files in os.walk(movdir):
        for filename in files:
            #find the name location and name of files
            path = os.path.join(root, filename)
            print path
            #file name and extension
            ARN, extension = os.path.splitext(filename)
            print ARN

            #Location of the corresponding folder in the new directory
            link = os.path.join(basedir,ARN)

            # if the folder already exists in new directory
            if os.path.exists(link):

                #this is the file location in the new directory
                file = os.path.join(basedir, ARN, ARN)
                linkfn = os.path.join(basedir, ARN, filename)

                if os.path.exists(linkfn):
                    i = 0
                    #if this file already exists in the folder
                    print "Path exists already"
                    while os.path.exists(file + "_" + str(i) + extension):
                        i+=1
                    print "Already 2x exists..."
                    print "Renaming"
                    shutil.copy(path, file + "_" + str(i) + extension)
                else:

                    shutil.copy(path, link)
                    print ARN + " " +  "Copied"
            else:
                print ARN + " " + "Not Found"

推薦答案

有時重新開始會更容易......如果有任何錯字,我很抱歉,我沒有時間徹底測試它.

Sometimes it is just easier to start over... I apologize if there is any typo, I haven't had the time to test it thoroughly.

movdir = r"C:Scans"
basedir = r"C:Links"
# Walk through all files in the directory that contains the files to copy
for root, dirs, files in os.walk(movdir):
    for filename in files:
        # I use absolute path, case you want to move several dirs.
        old_name = os.path.join( os.path.abspath(root), filename )

        # Separate base from extension
        base, extension = os.path.splitext(filename)

        # Initial new name
        new_name = os.path.join(basedir, base, filename)

        # If folder basedir/base does not exist... You don't want to create it?
        if not os.path.exists(os.path.join(basedir, base)):
            print os.path.join(basedir,base), "not found" 
            continue    # Next filename
        elif not os.path.exists(new_name):  # folder exists, file does not
            shutil.copy(old_name, new_name)
        else:  # folder exists, file exists as well
            ii = 1
            while True:
                new_name = os.path.join(basedir,base, base + "_" + str(ii) + extension)
                if not os.path.exists(new_name):
                   shutil.copy(old_name, new_name)
                   print "Copied", old_name, "as", new_name
                   break 
                ii += 1

這篇關于如果文件名已經存在,Python 將文件復制到新目錄并重命名的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 久操av在线 | 天天爽夜夜骑 | 欧美全黄 | 欧美99| 国产精品久久久久久久免费观看 | 天堂精品 | 成人久草| 色网站视频 | 日韩精品一区二区三区四区视频 | 久久久91精品国产一区二区三区 | 国家一级黄色片 | 在线欧美 | 国产免费a| 久久婷婷香蕉热狠狠综合 | 国产精品久久久亚洲 | 久久久久综合 | 91观看 | 夜夜草| 黄网站免费入口 | 欧美一区二区免费 | 毛片一级片 | av天天看| 欧美一级黄 | 中文字幕在线一区二区三区 | 欧美一级视频 | 国产日韩欧美精品一区二区三区 | 视频一区二区在线观看 | 亚洲日韩视频 | 欧美一区二区三区在线看 | 久久久国产一区二区三区四区小说 | 国产高清视频 | 日本a v在线播放 | 成人av鲁丝片一区二区小说 | 亚洲成色777777在线观看影院 | 国产精品欧美一区二区三区 | 亚洲色图在线观看 | 亚洲精选一区二区 | 国产一区二区三区 | 亚洲一区二区三区在线视频 | 在线一区二区三区 | 国产精品区二区三区日本 |