python - Cancel button on QFileDialog -


i running issue, when choose not save file , click "cancel" on system window program crash. here error receive:

traceback (most recent call last): file "basicemail.py", line 166, in save_content open(file_name[0], 'w') f: filenotfounderror: [errno 2] no such file or directory: '' 

this code using:

def save_content(self):     file_name = qtwidgets.qfiledialog.getsavefilename(self,'save file',os.getenv('home'))     if file_name:         open(file_name[0], 'w') f:             my_text = self.content.toplaintext()             f.write(my_text) 

thank in advance. know must missing something.

the if file_name: statement true since getsavefilename() function returns tuple, has following structure: (filename, filters), best name , verify string not empty.

def save_content(self):     file_name, _ = qtwidgets.qfiledialog.getsavefilename(self, 'save file', os.getenv('home'))     if file_name != "":         open(file_name, 'w') f:             my_text = self.content.toplaintext()             f.write(my_text) 

plus: can use qtcore.qdir.homepath() instead of os.getenv('home')


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

Qt QGraphicsScene is not accessable from QGraphicsView (on Qt 5.6.1) -

What is happening when Matlab is starting a "parallel pool"? -