我是一个有用的助手,可以为您翻译IT技术方面的中文内容。以下是需要翻译的内容:
我觉得现在这个方法有点笨拙:不太清楚
我认为下面的代码可能有效(我没有测试过,只在写这篇文章时想到了它!),但我不认为我应该使用
我正在编写一些代码,使文件名末尾的数字递增,直到不再覆盖现有文件。我正在创建几个文件,它们都具有相同的基本文件名但不同的扩展名,我都不希望覆盖任何一个文件。
简单版:
prefix = 'hello0'
while os.path.exists(prefix + '.abc') or os.path.exists(prefix + '.def') or os.path.exists(prefix + '.ghi'):
n = int(prefix[-1])
prefix = prefix[:-1] + str(n + 1) # I know this doesn't work when n reaches two digits; my full code involves a regular expression
当有超过几个扩展时,条件显然会变得非常冗长和丑陋。我将其抽象成一个for
循环。
我的版本:
prefix = 'hello0'
extensions = ('.abc', '.def', '.ghi') # there could be even more than this
condition = True
while condition:
condition = False
# if any of the paths still exist, set the condition back to True
for extension in extensions:
if os.path.exists(prefix + extension):
condition = True
n = int(prefix[-1])
prefix = prefix[:-1] + str(n + 1)
我觉得现在这个方法有点笨拙:不太清楚
while
循环在测试什么。是否可以动态地构建一个布尔表达式,而不是设置一个布尔值?我认为下面的代码可能有效(我没有测试过,只在写这篇文章时想到了它!),但我不认为我应该使用
eval
:prefix = 'hello0'
extensions = ('.abc', '.def', '.ghi')
test = 'False'
for extension in extensions:
test += " or os.path.exists(prefix + '" + extension + "')"
while eval(test):
n = int(prefix[-1])
prefix = prefix[:-1] + str(n + 1)
all()
方法。 - Francis Avila