缩进错误:Tab与空格混用问题排查,调整后仍无法解决
解决Python缩进错误:inconsistent use of tab and spaces in indentation
核心解决步骤
- 统一缩进规则:Python对缩进要求严格,必须全程使用同一种缩进方式——要么全用4个空格,要么全用制表符(Tab),官方推荐用4个空格。
- 批量清理混合缩进:用编辑器的替换功能一次性修正:
- 打开编辑器的替换面板(比如VS Code按Ctrl+H),勾选正则表达式选项
- 查找
\t,替换为(四个空格),执行全部替换 - 替换后再检查有没有零散的空格/Tab混合情况
- 锁定编辑器缩进设置:在编辑器里设置强制用空格缩进,比如VS Code中配置
editor.insertSpaces: true和editor.tabSize: 4,避免后续再出现混合缩进。
额外修正:代码中的文件读取错误
你的代码里打开密钥文件用了wb(二进制写入)模式,这会导致无法读取密钥内容,必须改成rb(二进制读取)模式,否则解密会失败。
修正缩进和文件模式后的完整代码
import os from cryptography.fernet import Fernet # get files files = [] for file in os.listdir(): if file == "ransomware.py" or file == "thekey.key" or file == "decrypt.py": continue if os.path.isfile(file): files.append(file) print(files) with open("thekey.key", "rb") as key: secretkey = key.read() secretphrase = "bootcon" user_phrase = input("Enter Phrase to decrypt your files\n") if user_phrase == secretphrase: for file in files: with open(file, "rb") as thefile: contents = thefile.read() contents_decrypted = Fernet(secretkey).decrypt(contents) with open(file, "wb") as thefile: thefile.write(contents_decrypted) print("thank you your files are decrypted") else: print("Wrong try again")
内容的提问来源于stack exchange,提问作者Rodrigo Gregorio
相关产品推荐
相关产品推荐

