writing to a file

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2008
Posts: 58
Reputation: adam291086 is an unknown quantity at this point 
Solved Threads: 0
adam291086 adam291086 is offline Offline
Junior Poster in Training

writing to a file

 
0
  #1
Jan 23rd, 2009
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

  1. #!/usr/bin/python
  2.  
  3. print "Creating a text file with the write() method."
  4. text_file = open("http://www.adamplowman.com/uni_project/log.txt", "w")
  5. text_file.write("Line 1\n")
  6. text_file.write("This is line 2\n")
  7. text_file.write("That makes this line 3\n")
  8. text_file.close()
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 960
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 220
Gribouillis's Avatar
Gribouillis Gribouillis is online now Online
Posting Shark

Re: writing to a file

 
0
  #2
Jan 23rd, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 1,613
Reputation: scru has a spectacular aura about scru has a spectacular aura about 
Solved Threads: 130
Featured Poster
scru's Avatar
scru scru is offline Offline
Posting Virtuoso

Re: writing to a file

 
0
  #3
Jan 23rd, 2009
I'm pretty sure the http protocol doesn't allow write access.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 58
Reputation: adam291086 is an unknown quantity at this point 
Solved Threads: 0
adam291086 adam291086 is offline Offline
Junior Poster in Training

Re: writing to a file

 
0
  #4
Jan 23rd, 2009
mmm i am running this from a website and that file in question is stored within another folder will it work then?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 960
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 220
Gribouillis's Avatar
Gribouillis Gribouillis is online now Online
Posting Shark

Re: writing to a file

 
0
  #5
Jan 23rd, 2009
It could work if you pass a file path to open, like uni_project/log.txt , but nothing that starts whith http://.
Last edited by Gribouillis; Jan 23rd, 2009 at 9:25 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 58
Reputation: adam291086 is an unknown quantity at this point 
Solved Threads: 0
adam291086 adam291086 is offline Offline
Junior Poster in Training

Re: writing to a file

 
0
  #6
Jan 23rd, 2009
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/proje...ing_to_file.py

is this possible?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 960
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 220
Gribouillis's Avatar
Gribouillis Gribouillis is online now Online
Posting Shark

Re: writing to a file

 
0
  #7
Jan 23rd, 2009
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.
Last edited by Gribouillis; Jan 23rd, 2009 at 9:55 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC