मैंने पहले से ही 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"
नहीं है, संरचना अलग है। उदाहरण के लिए, movdir संपत्ति की जानकारी के स्कैन है और सड़क के नाम से व्यवस्थित किया गया है और पीडीएफ अद्वितीय नाम के साथ नामित हैं। तो सी: \ स्कैन \ मुख्य सेंट \ 1234.pdf आधारित एक नई संरचना है जो किसी विशेष संपत्ति के लिए अपनी अनूठी आईडी द्वारा सभी जानकारी व्यवस्थित करने जा रही है। तो सी: \ लिंक \ 1234 और भविष्य में अतिरिक्त उप फ़ोल्डर्स हो सकते हैं लेकिन अभी के लिए मैं इसे सी: \ लिंक \ 1234 \ 1234.pdf – GISKid
पर कॉपी करने की उम्मीद कर रहा हूं ['filename_fix_existing (फ़ाइल नाम)'] (https://github.com/steveeJ/python-wget/blob/master/wget.py#L72) –