Choose folder to save a file

Asked

Viewed 2,854 times

2

I have this code that I select the place to save the file

    filename_2 = QFileDialog.getSaveFileName(self.dlg, "Select output file")
    self.dlg.pasta_saida.setText(filename_2)

however I would like to choose the folder to save the file inside how would you look?

2 answers

3


If you want to choose the location where the file will be saved you can use the method asksaveasfile module tkFileDielog, simply import the necessary libraries from the TKinter.

Here is an example for you:

import Tkinter, tkFileDialog

def salvaarquivo(texto):
    f = tkFileDialog.asksaveasfile(mode="w", )#A foi opcao definida para escrita.
    if f is None:
        return
    f.write(texto)
    f.close()

salvaarquivo(raw_input("Digite um texto para ser salvo:"))

Documentation of tkFileDialog.

Source.

Note: In case you don’t have the TKinter installed on your machine, you can download it and urge it following the instructions of the website of TKinter.

  • 1

    It worked on Pycharm, but on qgis it returns None...but thanks for the help anyway

0

You can try the following :

import TkInter, tkFileDialog
root = Tkinter.Tk()
dirNome = tkFileDialog.askdirectory(parent=root, initialdir="/",title ='Selecione a pasta')
  • could not import this library, I am using python to create a plugin in QGIS, this may be the reason?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.