📜  列表推导中的海象运算符 [Python 3.8.0] - Python 代码示例

📅  最后修改于: 2022-03-11 14:46:34.805000             🧑  作者: Mango

代码示例2
from os import getcwd, listdir, rename
import re

[rename(f'{p}{n}', f"{p}{''.join([w[:3] if len(w) > 3 else w for w in re.split('[-_. ]', n)[:-1]])}.{n.split('.')[-1]}")
 for n in listdir(p := f"{getcwd()}\\{input('Folder: ')}\\")]

When I run this code, I'm getting:
  File "C:\Users\1\Desktop\sn.py", line 4
    [rename(f'{p}{n}',f"{p}{''.join([w[:3] if len(w)>3 else w for w in re.split('[-_. ]',n)[:-1]])}.{n.split('.')[-1]}") for n in listdir(p:=f"{getcwd()}\\{input('Folder: ')}\\")]
                                                                                                                                         ^
SyntaxError: assignment expression cannot be used in a comprehension iterable expression

Process finished with exit code 1