Is this the best way to get unique version of filename w/ Python?

前端 未结 6 818
情话喂你
情话喂你 2021-02-05 12:36

Still \'diving in\' to Python, and want to make sure I\'m not overlooking something. I wrote a script that extracts files from several zip files, and saves the extracted files t

6条回答
  •  深忆病人
    2021-02-05 13:07

    if you don't care about readability, uuid.uuid4() is your friend.

    import uuid
    
    def unique_filename(prefix=None, suffix=None):
        fn = []
        if prefix: fn.extend([prefix, '-'])
        fn.append(str(uuid.uuid4()))
        if suffix: fn.extend(['.', suffix.lstrip('.')])
        return ''.join(fn)
    

提交回复
热议问题