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

前端 未结 6 874
情话喂你
情话喂你 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:08

    Two small changes...

    base_name, ext = os.path.splitext(file_name) 
    

    You get two results with distinct meaning, give them distinct names.

    file_name = "%s_%d%s" % (base_name, str(counter), ext)
    

    It isn't faster or significantly shorter. But, when you want to change your file name pattern, the pattern is on one place, and slightly easier to work with.

提交回复
热议问题