python 复制并重命名文件_Python将文件复制到新目录,如果文件名已存在,则重命名...

1586010002-jmsa.png

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

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.

e.g.

copy file 1234.pdf to folder in base directory 1234.

if 1234.pdf exists to name it 1234_1.pdf,

if another pdf is copied as 1234.pdf then it would be 1234_2.pdf.

Here is my code:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值