Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Help
#1
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
Reply
#2
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?
Reply
#3
What's the error?
Do you have permissions to write to that file?
Reply
#4
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. ")
Reply
#5
Quote:PermissionError
This says it all.
You need to figure out why.
There's nothing wrong with the code.
Reply
#6
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!!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020