2

So I feel like I'm probably using the wrong words to look around for code for this on Google/StackOverflow.

I'm building a script that (among other things) is going to be doing a lot of moving files around.

I currently have a little line to split the filename from the extension, and to add Filename + (Duplicate)+Extension if the file already exists in the directory.

However, I feel like there HAS to be a simple little one-liner that will essentially do (Duplicate), (Duplicate 1), (Duplicate 2), (Duplicate 3), etc. (essentially just changing that second number to the next number if a file exists with the current one).

What's the simple solution I'm too stupid to be able to figure out myself?

Sorry, it hadn't occurred to me that my current code might help people answer my question!

def destination(self, f): return os.path.abspath('.')+'/'+self.filename(f)+'/'+self.filename(f)+' (Duplicate)'+self.extension(f) if and os.path.isfile(os.path.abspath('.')+'/'+self.filename(f)+'/'+f) else os.path.abspath('.')+'/'+self.filename(f)+'/'+f 

I've used a slightly altered method of getting filename and extensions (essentially just to get around some rar parts and some folder issues). But 'self.filename(f) and self.extension(f) are basically just os.splittext(f)[0] and os.splittext(f)[1].

3
  • 2
    What's the not-simple-enough solution you already have? Commented Apr 11, 2012 at 14:13
  • @Robin Hood File rotation might be the term you're referring to. In Python the logging module is used to set up file rotation, and therefore have it taken care of by the logging module. I found this helpful in setting up the logging (and consequent file rotation) configuration. Not sure about the one liner to manually rename (or rotate) filenames. Commented Apr 11, 2012 at 14:28
  • I'm sorry if I wasn't clear, the problem isn't that my solution isn't simple enough, it's just that my solution can only deal with ONE duplicate, and the only way I can think of making it do two is to essentially just add another block of code that is the same but with 'Duplicate 1', and checks for both the regular and 'Duplicate'--something that would be completely ridiculous and also very long very quickly and would essentially end up being limited by how many of those I'd decided to add. Commented Apr 11, 2012 at 14:38

1 Answer 1

5

Of course, there is some one-liner to do this, but I cannot think of a very readable one. I'd go for something like this:

def alternative_names(filename): yield filename base, ext = os.path.splitext(filename) yield base + "(Duplicate)" + ext for i in itertools.count(1): yield base + "(Duplicate %i)" % i + ext target_name = next(alt_name for alt_name in alternative_names(target_name) if not os.path.exists(alt_name)) 

(This might basically be what you did, but you didn't post your code.)

Sign up to request clarification or add additional context in comments.

2 Comments

Beautiful! Agh, I have to use iterators :)
@Sven Marnach Nice code, my only problem is I can't figure out how to put this into a code that I can call elsewhere and that has a "sanity check" (I hope I'm using that term correctly) to make sure that if the path already exists (i.e. if the file is already in the right place) the destination should be the same. Right now this code sees that it exists and renames it....

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.