Python解密程序仅返回加密文本,请求协助排查故障
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 acceptsxas a parameter but usesyinternally (which isn't defined in the function scope). Worse, when you calltranslate(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 originaly. You need to assign the result back toyfor each replacement to take effect.Redundant
whileloop:
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
subfoldersandfilenamesare incorrect—os.walkalready 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
withstatement is the safe, Pythonic way to handle files.
- The nested loops over
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?
- Renamed function parameters for clarity:
translate(text)makes it obvious what input we're working with. - Assigned
replace()results back to a variable: Each replacement updates thedecryptedstring. - Removed the useless
whileloop: One pass through the rules is enough. - Fixed
os.walklogic: We ignore subfolders (using_as a throwaway variable) and check if the target filename exists in the current directory's files. - Used full file path:
os.path.joinensures we open the correct file, no matter where it is on the G: drive. - Added
withstatement: Automatically closes the file after reading, preventing resource leaks. - Added fallback message: Tells you if the file isn't found.
内容的提问来源于stack exchange,提问作者PY_NEWBIE

