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()

Recommended Answers

All 6 Replies

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.

I'm pretty sure the http protocol doesn't allow write access.

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

It could work if you pass a file path to open, like uni_project/log.txt , but nothing that starts whith http://.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.