954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

writing to a file

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()
adam291086
Junior Poster in Training
61 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

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.

Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

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

scru
Posting Virtuoso
1,629 posts since Feb 2007
Reputation Points: 975
Solved Threads: 140
 

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

adam291086
Junior Poster in Training
61 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

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

Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

i am struggling with this

so if the file i want to write to is located here
http://www.adamplowman.com/uni_project/log.txt

and my code to write to the file is running from here
http://adamplowman.com/cgi-bin/project/writing_to_file.py

is this possible?

adam291086
Junior Poster in Training
61 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

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.

Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You