2

python beginner here. I'm trying to create a script that recursively renames files in a folder according to set rules (no spaces, numbers, no special characters...). For that, I ask the user, by choosing in a list (all using inquirer) how they wants to rename their files. The problem is that I can't use the user's answers with inquirer. Here is the code:

import os
import re
import inquirer

def space():
    for file in os.listdir("."):
        filewspace = file.replace(" ","")
        if(filewspace != file):
            os.rename(file,filewspace)

def numbers():
    for file in os.listdir("."):
        os.rename(file, file.translate(str.maketrans('','','0123456789')))

questions = [
  inquirer.List('choices',
                message="How do you want to format?",
                choices=['Remove spaces', 'Remove numbers',
                'Remove specials',],
            ),
]
answers = inquirer.prompt(questions)

if (answers) == "space":
    space()
elif (answers) == "numbers":
    numbers()
#else:
    #specials()

When I try to print what is inside "answers" after the user choice, I get this back: https://i.stack.imgur.com/UrraB.jpg

I'm stuck at it... Can anyone help, please?

ALuTHIPA
  • 29
  • 4
  • 1
    Welcome to Stack Overflow. [Please don't post screenshots of text](https://meta.stackoverflow.com/a/285557/354577). They can't be searched or copied, or even consumed by users of adaptive technologies like screen readers. Instead, paste the code as text directly into your question. If you select it and click the `{}` button or Ctrl+K the code block will be indented by four spaces, which will cause it to be rendered as code. – ChrisGPT was on strike Jan 16 '22 at 21:56

2 Answers2

2

It seems that the variable answers contains a dictionary. So you have to change you if checks at the bottom with:

if answers["choices"] == "Remove spaces":
    space()
elif answers["choices"] == "Remove numbers":
    numbers()
...

That should have the desired functionality.

Camaendir
  • 472
  • 3
  • 8
1

Should work using the dictionary.

if (answers['choices'] == "Remove spaces"):
    space()
elif (answers['choices'] == "Remove numbers"):
    numbers()
#else:
    #specials()
Dani3le_
  • 1,257
  • 1
  • 13
  • 22