get a copy of a string leaving original intact

B

Bell, Kevin

I'm having trouble with something that seems like it should be simple.

I need to copy a file, say "abc-1.tif" to another directory, but if it's
in there already, I need to transfer it named "abc-2.tif" but I'm going
about it all wrong.

Here's what doesn't work: (I'll add the copy stuff from shutil after
figuring out the basic string manipulation.)


import os

source = r"C:\Source"
target = r"P:\Target"

files = os.listdir(source)

for f in files:
if os.path.isfile(target + "\\" + f): # if it already exists
print f + " exists"
s = f # i'd like a copy to
alter
s = s.replace("-1", "-2")
print "Altered it to be " + s
print source + "\\" + s, target + "\\" + s
else:
print f + " IS NOT THERE YET"
print source + "\\" + f, target + "\\" + f # use the original
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top