Python Forum
Python Help - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Python Help (/thread-2582.html)



Python Help - Sam - Mar-26-2017

I am having troubles writing a textfile in PyScripter. the code I am trying is:


filename = ("C:/RemoteSensing.txt")
f = open(filename, 'w')
f.write ("Check sun elevation in Landsat8 image of Baffin Island. ")
f.close()
But I get an error saying invalid mode ("w") or with my filename.

Moderator Larz60+: Added code tags -- see https://python-forum.io/misc.php?action=help&hid=25


RE: Python Help - Larz60+ - Mar-26-2017

I better way to do this:
with open("C:/RemoteSensing.txt", 'w') as f:
   f.write ("Check sun elevation in Landsat8 image of Baffin Island. ")
No close needed, done automatically if this format used.
Other than that, your code should have worked
What did you get for an error?


RE: Python Help - nilamo - Mar-26-2017

What's the error?
Do you have permissions to write to that file?


RE: Python Help - snippsat - Mar-26-2017

Writing to root C: give error PermissionError from PyScripter.
Works from command line.
Make a folder,you shouldn't write to root anyway.
with open("C:/bar/RemoteSensing.txt", 'w') as f:
  f.write ("Check sun elevation in Landsat8 image of Baffin Island. ")



RE: Python Help - Larz60+ - Mar-27-2017

Quote:PermissionError
This says it all.
You need to figure out why.
There's nothing wrong with the code.


RE: Python Help - Sam - Apr-01-2017

Thanks for your help everyone! I found out the problem was that we didn't have the correct file path.
Thanks for the help though!!