How to add an id to filename before extension?
Solution 1
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 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.
Solution 2
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")
Solution 3
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'
Solution 4
def append_id(filename):
parts = filename.split('.')
return "".join(parts[:-1])+ '_' + generate_id() + '.' + parts[-1]
Solution 5
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.
Related videos on Youtube
ss7
Updated on August 30, 2021Comments
-
ss7 over 2 years
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?-
BartoszKP almost 8 years
for name, ext in filename.split('.')
won't give you two successive elements of a collection inname
andext
, but will try to unpack each collection's element into two variables:name
andext
.
-
-
ss7 almost 8 yearsjust to add a string to files that have the same name when being moved
-
Stevoisiak over 5 yearsWill this work if
filename
is a path likeC:/path/filename.txt
-
DV82XL over 3 yearsI can't live without pathlib. Thanks for posting this!
-
user1330734 almost 3 yearsAs of June 2021, this is for me is the most up to date and simplest answer.