| | |
Back with more dumb questions!
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
2
#2 Nov 3rd, 2009
When you open a file, the cursor is at the beginning of the file. However
goes to the beginning of the file. Finally, read this.
python Syntax (Toggle Plain Text)
myfile.seek(0)
0
#3 Nov 4th, 2009
If you want to write something at the start of a file though you would need to do something like this
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
Python Syntax (Toggle Plain Text)
#opening the file in read ("r") mode f = open("file.txt",'r') #get all that is in the text file so far oldString = f.readlines() #and close it f.close() #delete it so we can reopen the file in write mode ('w') del f f=open("File.txt",'w') #the string we are adding newString = """ Hello This will go At the start""" #writing the new string for line in newString.split("\n"): f.write(line+'\n') #writing the old string for line in oldString: f.write(line+'\n') #closing the file 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.
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Check out my Site | and join us on IRC | Python Specific IRC
0
#4 Nov 4th, 2009
You can easily explore it yourself ...
Python Syntax (Toggle Plain Text)
# exploring Python's file random access text = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' fname = 'test103.txt' # create a test file fout = open(fname, "w") fout.write(text) fout.close() # open the test file for reading fin = open(fname, "r") # file positions are zero based # tell the current file position print( fin.tell() ) # 0 # read the byte at that position print( fin.read(1) ) # A # after reading 1 byte the position has advanced by 1 print( fin.tell() ) # 1 # read the next byte print( fin.read(1) ) # B print( fin.tell() ) # 2 # move the position to 10 fin.seek(10) # read the byte at position 10 print( fin.read(1) ) # K fin.close()
May 'the Google' be with you!
![]() |
Similar Threads
- Computer fails to start, constant beeping, fan seems to run at full speed (Cases, Fans and Power Supplies)
- News Story: Linux Needs to be "House" Trained. Not. (Linux Servers and Apache)
- Hello new to site and IT (Community Introductions)
- How to make a site sticky? (Promotion and Marketing Plans)
- Package post-install script problem (Shell Scripting)
- Compiling Questions (VB.NET)
- Windows Media Player 9 error messages (Windows NT / 2000 / XP)
- Error Message: Cannot find server; page cannot be displayed (Viruses, Spyware and other Nasties)
- ok, Here goes....an OLD Programmer.... (Community Introductions)
- Forum lurkers, introduce yourself ... !! (Community Introductions)
Other Threads in the Python Forum
- Previous Thread: Array Index Issue
- Next Thread: Word Frequency using Python
Views: 199 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for Python
anti approximation array avogadro beginner builtin cipher clear client code color converter countpasswordentry cturtle curved def dictionary drive dynamic examples excel file float format frange ftp function gui homework import input java lapse library line lines linux list lists loop microcontroller mouse multiple mysqldb mysqlquery newb number numbers output parsing path port prime program programming projects py2exe pygame pymailer pyqt python random recursion recursive redirect script scrolledtext singleton socket sqlite ssh stderr string strings subprocess sum syntax table terminal text textarea thread threading time tkinter tlapse tuple tutorial twoup ubuntu unicode unix urllib urllib2 variable web-scrape wikipedia windows word wxpython






