Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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
add comment

1 Answer

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')
os.path.abspath(f.name)

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')
os.path.abspath(f.name)
share|improve this answer
add comment

Your Answer

 
discard

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.