Back with more dumb questions!

Thread Solved

Join Date: Oct 2009
Posts: 12
Reputation: Kolz is an unknown quantity at this point 
Solved Threads: 0
Kolz Kolz is offline Offline
Newbie Poster

Back with more dumb questions!

 
0
  #1
19 Days Ago
Is there a method that, say, for example... you open a file, and usually the cursor would be at the end right? Is there a function or method that would set the cursor back to the beginning of the file?

Thanks!
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 887
Reputation: Gribouillis has a spectacular aura about Gribouillis has a spectacular aura about Gribouillis has a spectacular aura about 
Solved Threads: 209
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Practically a Posting Shark
 
2
  #2
19 Days Ago
When you open a file, the cursor is at the beginning of the file. However
  1. myfile.seek(0)
goes to the beginning of the file. Finally, read this.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 893
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 143
Sponsor
Paul Thompson's Avatar
Paul Thompson Paul Thompson is offline Offline
previously paulthom12345
 
0
  #3
19 Days Ago
If you want to write something at the start of a file though you would need to do something like this
  1. #opening the file in read ("r") mode
  2. f = open("file.txt",'r')
  3. #get all that is in the text file so far
  4. oldString = f.readlines()
  5. #and close it
  6. f.close()
  7.  
  8.  
  9. #delete it so we can reopen the file in write mode ('w')
  10. del f
  11. f=open("File.txt",'w')
  12.  
  13. #the string we are adding
  14. newString = """
  15. Hello
  16. This will go
  17. At the start"""
  18.  
  19. #writing the new string
  20.  
  21. for line in newString.split("\n"):
  22. f.write(line+'\n')
  23.  
  24. #writing the old string
  25. for line in oldString:
  26. f.write(line+'\n')
  27.  
  28. #closing the file
  29.  
  30. f.close()

That is untested code.. so im not quite sure how it will work, but hopefully it shows the concept i am trying to show

hope it helps
Last edited by Paul Thompson; 19 Days Ago at 2:35 am.
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,954
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 917
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
0
  #4
19 Days Ago
You can easily explore it yourself ...
  1. # exploring Python's file random access
  2.  
  3. text = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  4.  
  5. fname = 'test103.txt'
  6.  
  7. # create a test file
  8. fout = open(fname, "w")
  9. fout.write(text)
  10. fout.close()
  11.  
  12. # open the test file for reading
  13. fin = open(fname, "r")
  14.  
  15. # file positions are zero based
  16. # tell the current file position
  17. print( fin.tell() ) # 0
  18. # read the byte at that position
  19. print( fin.read(1) ) # A
  20. # after reading 1 byte the position has advanced by 1
  21. print( fin.tell() ) # 1
  22. # read the next byte
  23. print( fin.read(1) ) # B
  24. print( fin.tell() ) # 2
  25.  
  26. # move the position to 10
  27. fin.seek(10)
  28. # read the byte at position 10
  29. print( fin.read(1) ) # K
  30.  
  31. fin.close()
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC