| | |
Back with more dumb questions!
Thread Solved |
2
#2 21 Days Ago
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 20 Days Ago
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; 20 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
Check out my Site | and join us on IRC | Python Specific IRC
0
#4 20 Days Ago
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
- 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)
- Computer fails to start, constant beeping, fan seems to run at full speed (Cases, Fans and Power Supplies)
- 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
| Thread Tools | Search this Thread |
accessdenied advanced aliased argv beginner bits calling casino change changecolor clear command convert corners count csv cturtle cursor def definedlines dictionary digital dynamic dynamically enter event events external file float format frange function google homework i/o iframe import input jaunty java keyboard lapse line linux list lists loop matching microphone mouse multiple newb number numbers obexftp output parameters parsing path port prime programming projects py py2exe pygame pyopengl python random rational raw_input recursion return scrolledtext signal simple singleton skinning sprite string strings tails text threading time tlapse tuple tutorial ubuntu unicode urllib urllib2 valueerror variable voip web-scrape whileloop word wxpython






