def delete(): try: if set_ui.tabWidget.currentIndex() == 0: if set_ui.treeWidget.currentIndex().data() in book: logging.debug("detele {}".format(set_ui.treeWidget.currentIndex().data())) book.pop(set_ui.treeWidget.currentIndex().data()) address.pop(set_ui.treeWidget.currentIndex().data()) set_init() ui.update_tree() save() else: ui.statusbar.showMessage("选择地址是不可以的哦!把鼠标左移一点吧!") elif set_ui.tabWidget.currentIndex() == 1: logging.debug("detele {}".format(set_ui.listWidget.currentIndex().data())) book[set_ui.comboBox.currentText()].pop(set_ui.listWidget.currentIndex().data()) path = "{}/{}".format(address[set_ui.comboBox.currentText()], set_ui.listWidget.currentIndex().data()) dir = QDir() dir.rmdir(path) set_init() ui.update_tree() save() else: logging.debug("detele {}".format(set_ui.listWidget_2.currentIndex().data())) book[set_ui.comboBox_2.currentText()][set_ui.comboBox_3.currentText()].remove( set_ui.listWidget_2.currentIndex().data()) path = "{}/{}/{}.md".format(address[set_ui.comboBox_2.currentText()], set_ui.comboBox_3.currentText(), set_ui.listWidget_2.currentIndex().data()) dir = QDir() dir.remove(path) set_init() ui.update_tree() save() except IOError: logging.warning("failed to delete")
def copyFiles(self, src, dst, names=None, overwrite=True): """ Copy files from src directory to dst directory. Subfolders are ignored. If names is provided, copies only files matching those names. Symlinks are not copied """ moDebug("Copying files from {} to {}. Names: {}".format( src, dst, names)) srcDir = QDir(src, "", QDir.IgnoreCase, QDir.Files | QDir.NoSymLinks) dstDir = QDir(dst) if not srcDir.exists() or not dstDir.exists(): moWarn("Failed to copy files with non-existant directories") return False success = True if names: srcDir.setNameFilters(names) fileNames = srcDir.entryList() for name in fileNames: if dstDir.exists(name): if not overwrite: continue dstDir.remove(name) success = QFile.copy(srcDir.filePath(name), dstDir.filePath(name)) if not success: moWarn('Failed to copy "{}" from "{}" to "{}"'.format( name, src, dst)) return success
def removeDir(dirName): d = QDir(dirName) if d.exists(): for info in d.entryInfoList(QDir.Dirs | QDir.Files | QDir.NoDotAndDotDot): if info.isDir(): removeDir(info.absoluteFilePath()) else: d.remove(info.fileName()) d.rmdir(dirName)
def on_btnDir_remove_clicked(self): self.__showBtnInfo(self.sender()) sous = self.ui.editFile.text().strip() if sous == "": self.ui.textEdit.appendPlainText("请先选择一个文件") return parDir = self.ui.editDir.text().strip() if parDir == "": self.ui.textEdit.appendPlainText("请先选择一个目录") return dirObj = QDir(parDir) if dirObj.remove(sous): self.ui.textEdit.appendPlainText("成功删除文件:\n" + sous + "\n") else: self.ui.textEdit.appendPlainText("删除文件失败\n")
class EntryProviderParent(IEntryProvider): def __init__(self): self.parentDir = QDir() def setContext(self, context: str): self.setDirectory(context) def setDirectory(self, dirPath: str): self.parentDir = QDir(dirPath) def isEntryValid(self, entry: str) -> bool: # if already exists if entry in self.parentDir: return False return True def removeEntry(self, entry: str): if not self.parentDir.remove(entry): raise Exception("Failed to remove entry") def renameEnty(self, oldName: str, newName: str): if not self.parentDir.rename(oldName, newName): raise Exception("Rename Failed")