0

I get the file path from self.fileSystemModel.fileInfo(index).absoluteFilePath() deleting the filepath works, but zipping it doesn't... I have tried the zip code alone and it works.

The path provided by the self.fileSystemModel.fileInfo(index).absoluteFilePath() cant be zipped...how can I get a path from the QTreeView that can be zipped

here is my code

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
import os
from zipfile import ZipFile
from os.path import basename
import zipfile


import DiskCleaner_ui


class main(QMainWindow, DiskCleaner_ui.Ui_MainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)

        self.openMenu()

        systray_icon = QIcon("s.png")
        self.systray = QSystemTrayIcon(systray_icon, self)
        self.systray.setContextMenu(self.menu)
        self.systray.show()
        self.systray.showMessage("DC", "Started...", QSystemTrayIcon.Information)
        # self.closeapp.triggered.connect(self.close)

        self.Duplicate()
        self.Unused()
        self.Temp()



    def openMenu(self):
        self.menu = QMenu()
        self.restore = QAction("Restore", self)
        self.closeapp = QAction("Close", self)

        self.menu.addActions([self.restore, self.closeapp])


    def on_clicked(self, index):
        self.path = self.fileSystemModel.fileInfo(index).absoluteFilePath()
        print(self.path)
        self.temp_delete_button.clicked.connect(self.delete_file)
        self.duplicate_delete_button.clicked.connect(self.delete_file)
        self.unused_delete_button.clicked.connect(self.delete_file)

        self.temp_zip_button.clicked.connect(self.zip_file)



    def zip_file(self):
        if os.path.isfile(self.path):
            with ZipFile('C:\Windows\DC\zipfile.zip', 'w') as zip:
                zip.write(self.path, basename(self.path))

            print('file zipped successfully!')
        else:
            zf = zipfile.ZipFile('C:\Windows\DC\zipfile.zip', "w")
            for dirname, subdirs, files in os.walk(self.path):
                zf.write(dirname, basename(dirname))
                for filename in files:
                    zf.write(os.path.join(dirname, filename), basename(os.path.join(dirname, filename)))
            zf.close()
            print('All files/folders zipped successfully!')




    def delete_file(self):
        if os.path.exists(self.path):
            os.remove(self.path)
            self.systray.showMessage("DC", "Temporary file/folder Deleted", QSystemTrayIcon.Information)
            print('FIle Deleted...')
        else:
            pass




    def Temp(self):

        treeView = QTreeView()
        fileSystemModel = QFileSystemModel(treeView)
        fileSystemModel.setReadOnly(False)
        root = fileSystemModel.setRootPath(r'C:\Users\Black Laptop\Desktop\Py1')
        treeView.setModel(fileSystemModel)
        treeView.setRootIndex(root)
        treeView.setSortingEnabled(True)

        treeView.clicked.connect(self.on_clicked)

        clearAll_button = QPushButton("Clear all Files")
        clearAll_button.setFixedSize(90, 30)
        self.temp_delete_button = QPushButton('Delete')
        self.temp_delete_button.setFixedSize(90, 30)
        self.temp_zip_button = QPushButton('Zip file')
        self.temp_zip_button.setFixedSize(90, 30)

        Layout = QHBoxLayout(self)
        Layout.addWidget(clearAll_button)
        Layout.addWidget(self.temp_delete_button)
        Layout.addWidget(self.temp_zip_button)
        Layout.addWidget(treeView)


        self.Temp_Tab.setLayout(Layout)

    def Duplicate(self):

        treeView = QTreeView()
        self.fileSystemModel = QFileSystemModel(treeView)
        self.fileSystemModel.setReadOnly(False)
        root = self.fileSystemModel.setRootPath(r'C:\Users\Black Laptop\Desktop\Py2')
        treeView.setModel(self.fileSystemModel)
        treeView.setRootIndex(root)
        treeView.setSortingEnabled(True)

        treeView.clicked.connect(self.on_clicked)

        clearAll_button = QPushButton("Clear all Files")
        clearAll_button.setFixedSize(90, 30)
        self.duplicate_delete_button = QPushButton('Delete')
        self.duplicate_delete_button.setFixedSize(90, 30)
        zip_button = QPushButton('Zip file')
        zip_button.setFixedSize(90, 30)

        Layout = QHBoxLayout(self)
        Layout.addWidget(clearAll_button)
        Layout.addWidget(self.duplicate_delete_button)
        Layout.addWidget(zip_button)
        Layout.addWidget(treeView)

        self.Duplicate_Tab.setLayout(Layout)

    def Unused(self):

        treeView = QTreeView()
        fileSystemModel = QFileSystemModel(treeView)
        fileSystemModel.setReadOnly(False)
        root = fileSystemModel.setRootPath(r'C:\Users\Black Laptop\Desktop\Py3')
        treeView.setModel(fileSystemModel)
        treeView.setRootIndex(root)
        treeView.setSortingEnabled(True)

        treeView.clicked.connect(self.on_clicked)

        clearAll_button = QPushButton("Clear all Files")
        clearAll_button.setFixedSize(90, 30)
        self.unused_delete_button = QPushButton('Delete')
        self.unused_delete_button.setFixedSize(90, 30)
        zip_button = QPushButton('Zip file')
        zip_button.setFixedSize(90, 30)

        Layout = QHBoxLayout(self)
        Layout.addWidget(clearAll_button)
        Layout.addWidget(self.unused_delete_button)
        Layout.addWidget(zip_button)
        Layout.addWidget(treeView)


        self.UnUsed_Tab.setLayout(Layout)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    H = main()
    H.show()
    app.exec_()

i get this error...

Error C:/Users/Black Laptop/Desktop/Py1/1 - Copy (7).txt Traceback (most recent call last): File "test2.py", line 107, in zip_file with ZipFile(dpath, 'w') as zip: \n File "C:\Program Files (x86)\Python36-32\lib\zipfile.py", line 1090, in __init __ self.fp = io.open(file, filemode) PermissionError: [Errno 13] Permission denied: 'C:\Users\Black Laptop\Desktop

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
X-Black...
  • 1,376
  • 2
  • 20
  • 28
  • If the problem is not caused by the IDE then avoid using it, clearly the error is not generated by pycharm. – eyllanesc May 26 '18 at 22:43
  • its from the code bro...the **self,path** can be deleted but cant be zipped - thats my problem – X-Black... May 26 '18 at 22:46
  • Have you understood what I have told you? The question and answers should not only serve you, but the entire community, and the main means to search are the tags, choose them wisely. As you ask for help, we ask you for consideration. – eyllanesc May 26 '18 at 22:51
  • yea now i get...thanks....can u help – X-Black... May 26 '18 at 22:52
  • @X-Black... Do not run it with your IDE, open a CMD and execute it there. – eyllanesc May 26 '18 at 23:02
  • @PRMoureu everything works fine but the error comes up when i try to zip a file from the TreeView – X-Black... May 26 '18 at 23:03
  • then check the "Run" tab in Pycharm, or the cmd shell, you should have some traceback – PRMoureu May 26 '18 at 23:06
  • @eyllanesc tried it same error... – X-Black... May 26 '18 at 23:08
  • @X-Black... I did not say that I was going to solve the problem but that there you would see the error message, you can show the error message that is printed in the CMD. – eyllanesc May 26 '18 at 23:10
  • **Error** C:/Users/Black Laptop/Desktop/Py1/1 - Copy (7).txt Traceback (most recent call last): File "test2.py", line 107, in zip_file with ZipFile(dpath, 'w') as zip: \n File "C:\Program Files (x86)\Python36-32\lib\zipfile.py", line 1090, in __init __ self.fp = io.open(file, filemode) PermissionError: [Errno 13] Permission denied: 'C:\\Users\\Black Laptop\\Desktop – X-Black... May 26 '18 at 23:16
  • you are trying to compress files that do not have permissions. – eyllanesc May 26 '18 at 23:18
  • @eyllanesc i got it ...Thanks....the actual error was from the code...my dpath didn't specify a zip file..... – X-Black... May 26 '18 at 23:39

1 Answers1

0

dpath error...change to a user directory...new code

def zip_file(self,):
    dpath = r'C:\Users\Black Laptop\Desktop\DC.zip'
    if os.path.isfile(self.path):
        with ZipFile(dpath, 'w') as zip:
            zip.write(self.path, basename(self.path))

        print('file zipped successfully!')
    else:
        zf = zipfile.ZipFile(dpath, "w", )
        for dirname, subdirs, files in os.walk(self.path):
            zf.write(dirname, basename(dirname))
            for filename in files:
                zf.write(os.path.join(dirname, filename), basename(os.path.join(dirname, filename)))
        zf.close()
        print('All files/folders zipped successfully!')
X-Black...
  • 1,376
  • 2
  • 20
  • 28