类型错误:预期的 str、bytes 或 os.PathLike 对象,而不是元组 [英] TypeError: expected str, bytes or os.PathLike object, not tuple

查看:137
本文介绍了类型错误:预期的 str、bytes 或 os.PathLike 对象,而不是元组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在尝试创建一种带有 GUI 的加密程序.代码如下:

So I am trying to create a form of encryption program with a GUI. Here is the code:

import sys
from PyQt4 import QtGui, QtCore
import os
from Crypto.Hash import SHA256
from Crypto import Random
from Crypto.Cipher import AES

class Window(QtGui.QMainWindow):

    def __init__(self):
        super(Window, self).__init__()
        self.setGeometry(50, 50, 500, 300)
        self.setWindowTitle("Encryptionprogram")
        self.setWindowIcon(QtGui.QIcon('pythonicon.png'))

        self.container = QtGui.QWidget()
        self.setCentralWidget(self.container)
        self.container_lay = QtGui.QVBoxLayout()
        self.container.setLayout(self.container_lay)



        extractAction = QtGui.QAction("Leave", self)
        extractAction.setShortcut("Ctrl+Q")
        extractAction.setStatusTip("Leave the app")
        extractAction.triggered.connect(self.close_application)

        mainMenu = self.menuBar()
        fileMenu = mainMenu.addMenu('&File')
        fileMenu.addAction(extractAction)

        #Inputs
        self.Input = QtGui.QLineEdit("Filname", self)

        self.Input.setFixedWidth(200)
        self.Input.setFixedHeight(25)
        self.Input.move(20, 200)
        self.Input.setSizePolicy(QtGui.QSizePolicy.Fixed,
                                 QtGui.QSizePolicy.Fixed)

        self.Input2 = QtGui.QLineEdit("password", self)

        self.Input2.setFixedWidth(200)
        self.Input2.setFixedHeight(25)
        self.Input2.move(220, 200)
        self.Input2.setSizePolicy(QtGui.QSizePolicy.Fixed,
                                  QtGui.QSizePolicy.Fixed)

        self.home()

    def home(self):

        #Enter

        self.enter_btn = QtGui.QPushButton("Enter")
        self.container_lay.addWidget(self.enter_btn)
        self.enter_btn.clicked.connect(self.run1)

        self.checkBox = QtGui.QCheckBox('Krypter?', self)
        self.checkBox.move(0, 60)

        self.checkBox2 = QtGui.QCheckBox('Decrypt', self)
        self.checkBox2.move(100, 60)

        extractAction = QtGui.QAction(QtGui.QIcon('CloseIcon.png'), 'Close Program', self)
        extractAction.triggered.connect(self.close_application)

        self.toolBar = self.addToolBar("Extraction")
        self.toolBar.addAction(extractAction)



        self.show()


    def enlarge_window(self, state):
        if state == QtCore.Qt.Checked:
            self.setGeometry(50, 50, 1250, 600)
        else:
            self.setGeometry(50, 50, 500, 300)

    def close_application(self):
        choice = QtGui.QMessageBox.question(self, 'Attention!',
                                           "Close Program?",
                                           QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)
        if choice == QtGui.QMessageBox.Yes:
            print("Closing...")
            sys.exit()
        else:
            pass


    def closeEvent(self, event):
        event.ignore()
        self.close_application()

    def getKey(self, password):
        hasher = SHA256.new(password.encode('utf-8'))
        return hasher.digest()

    def run1(self):
        if self.checkBox.isChecked():
            Inputfilename = self.Input.text()
            Inputpassword = self.Input2.text()
            filename = str(Inputfilename)
            password = str(Inputpassword)
            print(filename, password)
            self.encrypt(self.getKey(password), filename)
            print("Done.")
        else:
            print("no work")


    def encrypt(self, key, filename):
        chunksize = 64 * 1024
        outputFile = "(Krypteret)", filename
        filesize = str(os.path.getsize(filename)).zfill(16)
        IV = Random.new().read(16)

        encryptor = AES.new(key, AES.MODE_CBC, IV)

        with open(filename, 'rb') as infile:
            with open(outputFile, 'wb') as outfile:
                outfile.write(filesize.encode('utf-8'))
                outfile.write(IV)

                while True:
                    chunk = infile.read(chunksize)

                    if len(chunk) == 0:
                        break
                    elif len(chunk) % 16 != 0:
                        chunk += b' ' * (16 - (len(chunk) % 16))

                    outfile.write(encryptor.encrypt(chunk))


    def decrypt(self, key, filename):
        chunksize = 64 * 1024
        outputFile = filename[11:]

        with open(filename, 'rb') as infile:
            filesize = int(infile.read(16))
            IV = infile.read(16)

            decryptor = AES.new(key, AES.MODE_CBC, IV)

            with open(outputFile, 'wb') as outfile:
                while True:
                    chunk = infile.read(chunksize)

                    if len(chunk) == 0:
                        break

                    outfile.write(decryptor.decrypt(chunk))
                outfile.truncate(filesize)





def run():
    app = QtGui.QApplication(sys.argv)
    GUI = Window()
    sys.exit(app.exec_())


run()

但我收到此错误

    Traceback (most recent call last):
  File "D:/Dokumenter - HDD/Python/GUI.py", line 119, in run1
    self.encrypt(self.getKey(password), filename)
  File "D:/Dokumenter - HDD/Python/GUI.py", line 134, in encrypt
    with open(outputFile, 'wb') as outfile:
TypeError: expected str, bytes or os.PathLike object, not tuple
Closing...

Process finished with exit code 0

我不知道如何解决这个问题,我不仅尝试在互联网上找到解决方案,而且还尝试修复它,只是在出现任何潜在错误后寻找解决方案,但此时我什么也没找到.如果有人可以解释错误和/或提出解决方案,那对我来说意味着整个世界.谢谢!

I dont know how to fix this I have tried to find a solution not only just on the internet but i have also tried to fix it just be lokking after any potential mistakes but at this moment i have found nothing. If anyone could explain the error and/or come with a solution it would mean the world to me. Thanks!

推荐答案

你定义 outputfile 如下:

You define outputfile as follows:

outputFile = "(Krypteret)", filename

这是一个元组,因此是错误的.不清楚你在这里的意思;也许您想在现有文件名前加上(Krypteret)"这个词?在这种情况下你应该这样做

which is a tuple, hence the error. It's not clear what you mean here; perhaps you wanted to prepend the word "(Krypteret)" to the existing filename? In which case you should do

outputFile = "(Krypteret)" + filename

(以后请尽量减少代码.错误完全在encrypt方法内,您应该刚刚发布该方法.)

(In future, please cut your code down to the minimum. The error is entirely within the encrypt method, you should have just posted that method.)

这篇关于类型错误:预期的 str、bytes 或 os.PathLike 对象,而不是元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆