Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am writing a QGIS Python Plugin. I am trying to write data to a file using

with open("filename.txt", "w") as f: 
    f.write("file text.")
QMessageBox.information(None, "Test", "Written to file.")

It runs the above and displays the message but it does not write to the file. Why is it not writing to the file? No exceptions are raised.

Thank-you in advance.

share|improve this question
up vote 2 down vote accepted

Python is doing what you tell it and that is to create a file in the current working directory which when you are running QGIS is your QGIS install location. Try running this in the Python console:

f = open("test.txt",'w')

What you should really do is tell open the path to where you want to store the file:

import os
p = os.path.join(os.path.dirname(__file__), 'test.txt')
f = open(p,'w')
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.