Walrus operator in list comprehensions [Python 3.8.0] 1

Walrus operator in list comprehensions [Python 3.8.0]

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

Here is what the above code is Doing:
1. It’s asking for a folder name.
2. It’s getting the current working directory.
3. It’s getting the list of files in the folder.
4. It’s renaming the files.

I’m not sure what’s wrong with the code.

Similar Posts