You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python解密程序仅返回加密文本,请求协助排查故障

Fixing Your Python Decryption Script

Hey there, let's dig into why your decryption script isn't working—there are a few key issues in the code that are keeping it from returning the decrypted text. Let's break them down one by one, then fix the code:

Key Issues in the Original Code

  • Undefined/incorrect variable handling in translate:
    Your function accepts x as a parameter but uses y internally (which isn't defined in the function scope). Worse, when you call translate(x), you're passing a file object instead of the actual text content you read from the file.

  • String replace() doesn't modify in-place:
    Python strings are immutable—y.replace(old, new) returns a new string instead of changing the original y. You need to assign the result back to y for each replacement to take effect.

  • Redundant while loop:
    The loop running 1000 times is unnecessary. str.replace() replaces all occurrences of the substring in one go, so a single pass through your replacement rules is enough.

  • Broken file search logic:

    • The nested loops over subfolders and filenames are incorrect—os.walk already handles recursive traversal, so you don't need to loop through subfolders separately.
    • You're trying to open files using just the filename, not the full path. This will fail unless the file is in your current working directory.
    • You don't close the file after reading it—using a with statement is the safe, Pythonic way to handle files.

Corrected Code

def translate(text):
    # Start with the input text
    decrypted = text
    # Apply all replacement rules—assign back to decrypted each time
    decrypted = decrypted.replace("quebrqerubfq92983rgh", "A")
    decrypted = decrypted.replace("837bfv8g24gh2783", "C")
    decrypted = decrypted.replace("9q37f93724f9732f", "E")
    decrypted = decrypted.replace("eqvrgb8rgbb452", "G")
    decrypted = decrypted.replace("1974f17934hf7h", "I")
    decrypted = decrypted.replace("1973f9734fbviu3", "K")
    decrypted = decrypted.replace("urhg9327hg9283g", "M")
    decrypted = decrypted.replace("18374f19374h983h", "O")
    decrypted = decrypted.replace("94fh93184hf91834f", "Q")
    decrypted = decrypted.replace("9rugueirgquierg", "S")
    decrypted = decrypted.replace("294uv94ugh293ugh928", "U")
    decrypted = decrypted.replace("29gh3i1fb94h9834h", "W")
    decrypted = decrypted.replace("193fh18934hfhb", "Y")
    decrypted = decrypted.replace("q9eurfuerhg93458htg", "B")
    decrypted = decrypted.replace("feugb2er9gh923gh", "D")
    decrypted = decrypted.replace("3ourv9u3rv93uv", "F")
    decrypted = decrypted.replace("v19u3rf9831hf89134h", "H")
    decrypted = decrypted.replace("913ufh9183hf91834", "J")
    decrypted = decrypted.replace("31uhf91384hf9834f", "L")
    decrypted = decrypted.replace("319fuh19hgf91834", "N")
    decrypted = decrypted.replace("fu3bf91u34bf1u34f", "P")
    decrypted = decrypted.replace("8134f87134fg13", "R")
    decrypted = decrypted.replace("f319u4f319u4hf3491", "T")
    decrypted = decrypted.replace("vq3yrbqbfuf", "V")
    decrypted = decrypted.replace("jirfbqiweufwqr440", "X")
    decrypted = decrypted.replace("qiuefqweurewiub", "Z")
    return decrypted

import os

target_filename = input("Enter the filename to decrypt: ")

# Walk through all directories on G: drive
for foldername, _, filenames in os.walk("g:"):
    if target_filename in filenames:
        # Build the full path to the file
        file_path = os.path.join(foldername, target_filename)
        # Use with statement to safely read the file
        with open(file_path, "r") as f:
            encrypted_text = f.read()
            decrypted_result = translate(encrypted_text)
            print("Decrypted content:\n", decrypted_result)
        # Exit after finding and processing the file (remove if you want to find multiple matches)
        break
else:
    print(f"File {target_filename} not found on G: drive.")

What Changed?

  1. Renamed function parameters for clarity: translate(text) makes it obvious what input we're working with.
  2. Assigned replace() results back to a variable: Each replacement updates the decrypted string.
  3. Removed the useless while loop: One pass through the rules is enough.
  4. Fixed os.walk logic: We ignore subfolders (using _ as a throwaway variable) and check if the target filename exists in the current directory's files.
  5. Used full file path: os.path.join ensures we open the correct file, no matter where it is on the G: drive.
  6. Added with statement: Automatically closes the file after reading, preventing resource leaks.
  7. Added fallback message: Tells you if the file isn't found.

内容的提问来源于stack exchange,提问作者PY_NEWBIE

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 05:24:44