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