2013-08-22 3 views
11

मैंने पहले से ही this thread पढ़ा है, लेकिन जब मैं इसे अपने कोड में कार्यान्वित करता हूं तो यह केवल कुछ पुनरावृत्तियों के लिए काम करता है।पायथन प्रतिलिपि फ़ाइलों को एक नई निर्देशिका में कॉपी करें और नाम बदलें कि फ़ाइल का नाम पहले से मौजूद है

मैं मुख्य रूप से पीडीएफ फ़ाइलों (एक अद्वितीय आईडी से मेल खाते हुए) को किसी अन्य निर्देशिका (बेस निर्देशिका) से मेल खाने वाले फ़ोल्डर (संबंधित अद्वितीय आईडी के साथ) की प्रतिलिपि बनाने के लिए पाइथन का उपयोग कर रहा हूं। । मैंने shutil.copy का उपयोग शुरू किया लेकिन यदि डुप्लीकेट हैं तो यह मौजूदा फ़ाइल को ओवरराइट करता है।

मैं यह देखने के लिए संबंधित फ़ोल्डर को खोजने में सक्षम होना चाहता हूं कि फ़ाइल पहले से मौजूद है या नहीं, और यदि एक से अधिक होता है तो इसे नाम दें।

उदा।

  • प्रतिलिपि फ़ाइल 1234.pdf आधार निर्देशिका में फ़ोल्डर में 1234.
  • अगर 1234.pdf यह 1234_1.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" 
+0

नहीं है, संरचना अलग है। उदाहरण के लिए, movdir संपत्ति की जानकारी के स्कैन है और सड़क के नाम से व्यवस्थित किया गया है और पीडीएफ अद्वितीय नाम के साथ नामित हैं। तो सी: \ स्कैन \ मुख्य सेंट \ 1234.pdf आधारित एक नई संरचना है जो किसी विशेष संपत्ति के लिए अपनी अनूठी आईडी द्वारा सभी जानकारी व्यवस्थित करने जा रही है। तो सी: \ लिंक \ 1234 और भविष्य में अतिरिक्त उप फ़ोल्डर्स हो सकते हैं लेकिन अभी के लिए मैं इसे सी: \ लिंक \ 1234 \ 1234.pdf – GISKid

+0

पर कॉपी करने की उम्मीद कर रहा हूं ['filename_fix_existing (फ़ाइल नाम)'] (https://github.com/steveeJ/python-wget/blob/master/wget.py#L72) –

उत्तर

6

कभी-कभी इसे शुरू करना आसान होता है ... यदि कोई टाइपो है तो मैं क्षमा चाहता हूं, मेरे पास इसका परीक्षण करने का समय नहीं है।

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

इसके लिए धन्यवाद, जब मैं इसे चलाता हूं तो मुझे एक त्रुटि मिलती है "ii" परिभाषित नहीं किया जाता है। ऐसा इसलिए हो सकता है क्योंकि मैं 2.7 का उपयोग कर रहा हूं (यह 3 से अधिक संगत है।एक्सजीजीआईएस के साथ एक्स जिसे कोड के साथ बाद में एकीकृत किया जा सकता है) – GISKid

+2

नहीं, यह है कि मैं गड़बड़ कर रहा हूं। यह सूचकांक = 0 के बजाय ii = 1 होना चाहिए (शून्य के बजाय, जैसा कि हमने दूसरे उत्तर में कहा था)। – Jblasco

0

मैं कहूंगा कि आप एक खरोज समस्या है, कम से कम के रूप में आप इसे यहाँ लिखा है:

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

होना चाहिए:

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

इसे देखें, कृपया!

+0

दुर्भाग्यवश, कुछ भी नहीं बदला। – GISKid

+0

ठीक है, हमें यह जानने की आवश्यकता होगी कि वास्तव में क्या काम नहीं कर रहा है। – Jblasco

+0

और यदि आप अपने द्वारा किए गए परिवर्तनों के साथ थोड़ा कोड अपडेट कर सकते हैं, भले ही वे काम नहीं करते हैं, तो यह बहुत अच्छा होगा। – Jblasco

0

मैं हमेशा समय स्टाम्प का उपयोग - तो संभव नहीं है, कि फ़ाइल पहले से मौजूद:

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) 
संबंधित मुद्दे