python初學者在這里。我正在嘗試創建一個腳本,該腳本根據設定的規則(沒有空格、數字、沒有特殊字符......)遞回地重命名檔案夾中的檔案。為此,我通過在串列中選擇(全部使用查詢器)來詢問用戶他們想要如何重命名他們的檔案。問題是我不能將用戶的答案與詢問者一起使用。這是代碼:
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()
當我嘗試在用戶選擇后列印“答案”中的內容時,我得到了回復: https ://i.stack.imgur.com/UrraB.jpg
我被困住了......有人可以幫忙嗎?
uj5u.com熱心網友回復:
該變數似乎answers包含字典。因此,您必須if在底部更改檢查:
if answers["choices"] == "Remove spaces":
space()
elif answers["choices"] == "Remove numbers":
numbers()
...
那應該具有所需的功能。
uj5u.com熱心網友回復:
應該使用字典。
if (answers['choices'] == "Remove spaces"):
space()
elif (answers['choices'] == "Remove numbers"):
numbers()
#else:
#specials()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/416142.html
標籤:
