Ben*_*son 6 python boolean-expression
我正在编写一些代码来增加文件名末尾的数字,直到它不再覆盖现有文件.我正在创建几个文件,所有文件都具有相同的基本文件名但不同的扩展名,我都不想覆盖它们.
朴素版:
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
Run Code Online (Sandbox Code Playgroud)
当有超过几个扩展时,这种情况显然会变得非常漫长和丑陋.我把它抽象成一个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)
Run Code Online (Sandbox Code Playgroud)
我仍觉得这有点笨重:它还不完全清楚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)
Run Code Online (Sandbox Code Playgroud)
您可能希望将any()内置函数与生成器一起使用:
while any(os.path.exists(prefix + extension) for extension in extensions):
# then increment prefix and try again, as in your example code
Run Code Online (Sandbox Code Playgroud)
这种计算True或False你需要用一个简单的语法.
一般情况下,如果我曾经使用感受的诱惑eval()像Python这样的动态语言,那么就意味着有一个重要的语言功能,我需要了解.动态语言应该使代码变得美观,而不会出现编写和维护代码的灾难 - 编写更多代码 - 所以你在这里通过询问你的方法做了正确的事情.