def encrypt_decrypt_fileinput_file_path output_file_path key try with openinput_file_path rb as input_file openoutput_file_path wb as output_file text = input_fileread
根据错误提示,“int” object has no attribute 'isalpha',说明代码中使用了一个整数类型的变量,但却调用了字符串类型的方法isalpha(),导致出错。因此,需要查找代码中哪个变量是整数类型,然后将其转换为字符串类型。
在代码中,可以发现在for循环中,c变量是一个整数类型,因此需要将其转换为字符串类型。修改代码如下:
c = chr(text[i]) # 将整数类型的变量c转换为字符串类型
完整修改后的函数代码如下:
def encrypt_decrypt_file(input_file_path, output_file_path, key): try: with open(input_file_path, 'rb') as input_file, open(output_file_path, 'wb') as output_file: text = input_file.read() result = "" n = len(key) for i in range(len(text)): c = chr(text[i]) # 将整数类型的变量c转换为字符串类型 if c.isalpha(): shift = ord(key[i % n]) - ord('a') if c.islower(): result += chr((ord(c) - ord('a') + shift) % 26 + ord('a')) else: result += chr((ord(c) - ord('A') + shift) % 26 + ord('A')) else: result += c output_file.write(result.encode('utf-8')) return True except Exception as e: messagebox.showerror("Error", str(e)) return False
请注意,修改后的代码仅解决了“int” object has no attribute 'isalpha'的错误,但并不保证整个程序的正确性和安全性。在实际应用过程中,需要对程序进行充分测试和验证,确保程序的正确性和安全性
AI 优化版
原文地址:https://www.cveoy.top/t/topic/hc2j 著作权归作者所有。请勿转载和采集!