I am trying to write some simple information to a file and i am getting the error message "No such file or directory when i know full well it exists. If you take the url http://www.adamplowman.com/uni_project/log.txt you get the file

heres the code

#!/usr/bin/python

print "Creating a text file with the write() method."
text_file = open("http://www.adamplowman.com/uni_project/log.txt", "w")
text_file.write("Line 1\n")
text_file.write("This is line 2\n")
text_file.write("That makes this line 3\n")
text_file.close()

It's because the builtin function open can only open a file in your filesystem and not an url. If you want to rewrite a file in your website, you should first write a file in your local file system and then use ftp or some other protocol to copy the file to your website.

mmm i am running this from a website and that file in question is stored within another folder will it work then?

Did you try something like "../uni_project/log.txt" ? Also you could use os.getcwd() in your program to see the path to the current dir. It would help build a relative path to your file.

This article has been dead for over six months. Start a new discussion instead.