32

I have a filename: name.ext

I want to do the following:

name + id + '.' + ext for name, ext in filename.split()

or find a better way to take a filename and add a random 7 character string to the end before the extension.

Here is what I have so far:

def generate_id(size=7, chars=string.ascii_uppercase + string.digits):
    return ''.join(random.choice(chars) for _ in range(size))

def append_id(filename):
    return (name + '_' + generate_id() + '.' + ext for name, ext in filename.split('.'))

but it treats it as a generator expression, which is not my intended result.

What would be the correct way to write the append_id function?

2
  • 1
    for name, ext in filename.split('.') won't give you two successive elements of a collection in name and ext, but will try to unpack each collection's element into two variables: name and ext.
    – BartoszKP
    May 27, 2016 at 15:58
  • Yeah now I see the problem. Was trying to do a list comp
    – ss7
    May 27, 2016 at 15:59

6 Answers 6

42

To do it in one line you can try:

def append_id(filename):
    return "{0}_{2}.{1}".format(*filename.rsplit('.', 1) + [generate_id()])

It's not very readable, though.

Most language implementations provide functions to deal with file names, and Python is no exception. You should use os.path.splitext:

def append_id(filename):
  return "{0}_{2}{1}".format(*os.path.splitext(filename) + (generate_id(),))

Note that the second version needs two additional modifications:

  • splitext returns a tuple not a list, so we need to wrap the result of generate_id with a tuple
  • splitext retains the dot, so you need to remove it from the format string

Still, I wouldn't struggle to have a oneliner here - see the next answer for more readable solutions.

Python 3.4 introduces pathlib module and you can use it like this:

from pathlib import Path

def append_id(filename):
  p = Path(filename)
  return "{0}_{2}{1}".format(p.stem, p.suffix, generate_id())

This will work for filenames without preceding path only. For files with paths use this:

from pathlib import Path

def append_id(filename):
  p = Path(filename)
  return "{0}_{2}{1}".format(Path.joinpath(p.parent, p.stem), p.suffix, generate_id())

In Python 3.9 there is also with_stem, which might be the most suitable choice for this case.

0
36

I'd suggest something plain and simple - use os.path.splitext to retrieve basename and extension, and after that simple merge all result components via str.format method.

import os
import random
import string

def generate_id(size=7, chars=string.ascii_uppercase + string.digits):
    return ''.join(random.choice(chars) for _ in range(size))

def append_id(filename):
    name, ext = os.path.splitext(filename)
    return "{name}_{uid}{ext}".format(name=name, uid=generate_id(), ext=ext)

Some testcases:

append_id("hello.txt")
append_id("hello")
append_id("multiple.dots.in.file")
1
  • Will this work if filename is a path like C:/path/filename.txt
    – Stevoisiak
    Sep 19, 2018 at 15:15
20

Since Python 3.4, a good tool for working with paths is pathlib. This treats paths as objects instead of strings and offers a powerful interface.

Since Python 3.9, a new method - with_stem was added, so your function can be:

from pathlib import Path

def append_id(filename):
    path = Path(filename)
    return path.with_stem(f"{path.stem}_{generate_id()}")

For older versions you can still use the with_name method:

from pathlib import Path

def append_id(filename):
    path = Path(filename)
    return path.with_name(f"{path.stem}_{generate_id()}{path.suffix}")

Those functions are generalized and will work also on full/relative paths and not just file names:

>>> append_id("sample.text")
PosixPath('sample_PBMQBFD.text')

>>> append_id("/a/b/c/sample.text")
PosixPath('/a/b/c/sample_UMTYXCP.text')

Note that a platform-specific Path object is returned. You can always just convert it back to a string with str():

>>> str(append_id("/a/b/c/sample.text"))
'/a/b/c/sample_O1LPTEC.text'
0
3
def append_id(filename):
    parts = filename.split('.')
    return "".join(parts[:-1])+ '_' + generate_id() + '.' + parts[-1]
2

your one line answer with the random generation -

map(lambda x :x.split(".")[0] + "_" + hashlib.md5(("%.15f" % time.time())+"_"+''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(5))).hexdigest()[:7]+"."+x.split(".")[1], filenames)

here you can input filenames as a list

just used a random id generation function which takes time and and random string of 5 characters md5's it and takes the first 7 characters from that.

It's not very readable but since you asked for a one line solution, I couldn't think of a more elaborate way.

2

To fix your generate_id code, you need a list comprehension.

def generate_id(size=7, chars=string.ascii_uppercase + string.digits):
    return '_' + ''.join([random.choice(chars) for _ in range(size)])

Are you using python just to rename files? You can use a command line program like pwgen instead.

mv $filename ${filename/%.ext/_$(pwgen 6 1).ext}

If you are writing to file(s) from Python, consider using tempfile.NamedTemporaryFile with the appropriate options. (delete=False, dir=".", prefix="...", suffix="...")

1
  • just to add a string to files that have the same name when being moved
    – ss7
    May 27, 2016 at 16:08

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.