2013-08-22 73 views
11

Tôi đã đọc this thread nhưng khi tôi triển khai nó vào mã của tôi, nó chỉ hoạt động cho một vài lần lặp.Sao chép tệp Python vào một thư mục mới và đổi tên nếu tên tệp đã tồn tại

Tôi đang sử dụng python để lặp qua thư mục (cho phép gọi thư mục di chuyển) để sao chép tập tin pdf chủ yếu (khớp với ID duy nhất) sang thư mục khác (thư mục cơ sở) vào thư mục phù hợp (với ID duy nhất tương ứng) . Tôi bắt đầu sử dụng shutil.copy nhưng nếu có trùng lặp, nó sẽ ghi đè tệp hiện có.

Tôi muốn có thể tìm kiếm thư mục tương ứng để xem tệp đã tồn tại hay chưa và đặt tên cho nó nếu có nhiều hơn một lần xuất hiện.

ví dụ:

  • copy file 1234.pdf vào thư mục trong thư mục cơ sở 1234.
  • nếu 1234.pdf tồn tại để đặt tên cho nó 1234_1.pdf,
  • nếu pdf khác được sao chép như 1234.pdf sau đó nó sẽ là 1234_2 .pdf.

Đây là mã của tôi:

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" 
+0

Không, cấu trúc là khác nhau. Ví dụ, movdir là quét thông tin thuộc tính và đã được sắp xếp theo tên đường phố và các tệp pdf được đặt tên bằng id duy nhất. Vì vậy, C: \ Scans \ Main St \ 1234.pdf Người thừa kế là một cấu trúc mới sắp xếp tất cả thông tin cho một thuộc tính cụ thể bằng ID duy nhất của nó. Vì vậy, C: \ Links \ 1234 và có thể có các thư mục phụ bổ sung trong tương lai nhưng bây giờ tôi chỉ hy vọng sao chép nó vào C: \ Links \ 1234 \ 1234.pdf – GISKid

+0

kiểm tra ['filename_fix_existing (filename)'] (https://github.com/steveeJ/python-wget/blob/master/wget.py#L72) –

Trả lời

6

Đôi khi nó chỉ dễ dàng hơn để bắt đầu lại ... Tôi xin lỗi nếu có lỗi đánh máy nào, tôi chưa có thời gian để kiểm tra kỹ lưỡng.

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 os.path.exists(newname): 
        shutil.copy(old_name, new_name) 
        print "Copied", old_name, "as", new_name 
        break 
       ii += 1 
+0

Cảm ơn điều này, khi tôi chạy này tôi nhận được một lỗi nói rằng "ii" không được xác định. Có thể là vì tôi đang sử dụng 2,7 (nó tương thích trên 3.x với ArcGIS có thể được tích hợp sau đó với mã) – GISKid

+2

Không, đó là tôi đã sai lầm. Nó phải là ii = 1 (thay vì 0, như chúng ta đã nói trong câu trả lời khác) thay vì chỉ số = 0. – Jblasco

0

tôi sẽ nói bạn có một vấn đề thụt đầu dòng, ít nhất là như bạn đã viết nó ở đây:

while not os.path.exists(file + "_" + str(i) + extension): 
    i+=1 
    print "Already 2x exists..." 
    print "Renaming" 
    shutil.copy(path, file + "_" + str(i) + extension) 

nên là:

while os.path.exists(file + "_" + str(i) + extension): 
    i+=1 
print "Already 2x exists..." 
print "Renaming" 
shutil.copy(path, file + "_" + str(i) + extension) 

Hãy khám phá điều này!

+0

Nó không thay đổi bất cứ điều gì, thật không may. – GISKid

+0

Ok, chúng tôi sẽ cần phải biết chính xác những gì không hoạt động, sau đó. – Jblasco

+0

Và nếu bạn có thể cập nhật một chút mã với những thay đổi bạn đã thực hiện, ngay cả khi chúng không hoạt động, nó sẽ rất tuyệt. – Jblasco

0

tôi luôn luôn sử dụng thời gian-tem - vì vậy nó không thể, mà các tập tin đã tồn tại:

import os 
import shutil 
import datetime 

now = str(datetime.datetime.now())[:19] 
now = now.replace(":","_") 

src_dir="C:\\Users\\Asus\\Desktop\\Versand Verwaltung\\Versand.xlsx" 
dst_dir="C:\\Users\\Asus\\Desktop\\Versand Verwaltung\\Versand_"+str(now)+".xlsx" 
shutil.copy(src_dir,dst_dir)