How to add an id to filename before extension?
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 ofgenerate_id
with a tuplesplitext
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.
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'
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")