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?
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 tuplesplitext
retains the dot, so you need to remove it from the format stringStill, 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With