943,962 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 306
  • Python RSS
Nov 3rd, 2009
0

Back with more dumb questions!

Expand Post »
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!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Kolz is offline Offline
12 posts
since Oct 2009
Nov 3rd, 2009
2
Re: Back with more dumb questions!
When you open a file, the cursor is at the beginning of the file. However
python Syntax (Toggle Plain Text)
  1. myfile.seek(0)
goes to the beginning of the file. Finally, read this.
Reputation Points: 930
Solved Threads: 668
Posting Maven
Gribouillis is online now Online
2,655 posts
since Jul 2008
Nov 4th, 2009
0
Re: Back with more dumb questions!
If you want to write something at the start of a file though you would need to do something like this
Python Syntax (Toggle Plain Text)
  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; Nov 4th, 2009 at 2:35 am.
Reputation Points: 264
Solved Threads: 183
Veteran Poster
Paul Thompson is offline Offline
1,095 posts
since May 2008
Nov 4th, 2009
0
Re: Back with more dumb questions!
You can easily explore it yourself ...
Python Syntax (Toggle Plain Text)
  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()
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Array Index Issue
Next Thread in Python Forum Timeline: palindrome





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC