#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Location
    Virginia
    Posts
    98
    Rep Power
    10

    Question Writing to a file


    When writing to a file, is there a way to add the stuff you want to add to a specific area in a document like a line number? Help please .
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    You can use the file() objects seek() method to move around within the file although it is much easier to read in the data, perform whatever actions you need to perform and write it back to the file i.e.

    Code:
    def insert(fileName,line, text):
        lines = file(fileName, 'r').readlines()
        lines.insert(line, text)
        file(fileName, 'w').writelines(lines)
    And here it is in action:

    Code:
    >>> print file('C:/test.txt', 'r').read()
    Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)] on win32
    Type "copyright", "credits" or "license()" for more information.
    
        ****************************************************************
        Personal firewall software may warn about the connection IDLE
        makes to its subprocess using this computer's internal loopback
        interface.  This connection is not visible on any external
        interface and no data is sent to or received from the Internet.
        ****************************************************************
        
    IDLE 1.0.3
    >>> insert('C:/test.txt', 5, '\n\n\nSample Text\n\n\n')
    >>> print file('C:/test.txt', 'r').read()
    Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)] on win32
    Type "copyright", "credits" or "license()" for more information.
    
        ****************************************************************
        Personal firewall software may warn about the connection IDLE
    
    
    
    Sample Text
    
    
        makes to its subprocess using this computer's internal loopback
        interface.  This connection is not visible on any external
        interface and no data is sent to or received from the Internet.
        ****************************************************************
        
    IDLE 1.0.3
    >>>
    Simply: this reads the contents of the file into a list via the readlines() method then uses the lists insert() method to add the text at the desired location [line]. The lines are then written to the file using writelines().

    Hope this helps,

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Location
    Virginia
    Posts
    98
    Rep Power
    10
    Awesome, thanks alot!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Location
    Virginia
    Posts
    98
    Rep Power
    10
    I am trying to add HTML to the file using user defined variables. Its not working an dgiving me the error:
    SyntaxError: EOL while scanning single-quoted string

    I checked the quotes and they look fine. Here is the code:

    Code:
    #This program adds a new update to the front page of www.toonhog.com/home
    #If you are given the right to use this software, please use it #responisbly
    
    #Gather data about post
    
    author = raw_input("Author: ")
    
    title = raw_input("Title: ")
    
    body = raw_input("Body: ")
    
    
    password = raw_input("Enter the password given to you: ")
     
    if password == '******':
    
    	print "Congratulations. The news post will be added automatically. Please wait, though, as this may take a few moments." 
    
    # ADD NEWS POST TO FILE
    
    open("index.htm" , 'w')
    
    insert('index.htm', 5, '
    
    
    
    <DIV style="position:relative; left:125px; border-color:666666; width:640px; border-style:solid">
    
    <center><h2><u>'title'</u></h2> <DIV style="text-align:right;"> <br>Posted by 'author'</DIV><br>
    
    'body'
    
    
    </center>
    
    </DIV><br><br>
    
    
    
    
    
    ')
    
    #DONE ADDING POST
    
    else:
    
    	print "Sorry. The password you entered was not correct. Please contact an administrator if you are having trouble (which you are)." 
    
    
    quit = raw_input("Press any key to continue...")
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    206
    Rep Power
    11
    The problem is that you have written a string like 'This is a multiline string with a 'bla' quote in it.'

    You cannot nest quotes in a quote (except someting like "this 'is' a test" or 'this "is" a test').

    Multilinestrings begin and end with three " (see below)

    Code:
    # ...
    
    if password == '******':
    
      print "Congratulations. The news post will be added automatically. Please wait, though, as this may take a few moments."
    
      # ADD NEWS POST TO FILE
    
      open("index.htm" , 'w')
    
      insert('index.htm', 5, """
    <DIV style="position:relative; left:125px; border-color:666666; width:640px; border-style:solid">
    <center><h2><u>'title'</u></h2> <DIV style="text-align:right;"> <br>Posted by 'author'</DIV><br>
    'body'
    </center>
    </DIV><br><br>
    """)
    
    # ...
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Location
    Virginia
    Posts
    98
    Rep Power
    10
    Well i checked all that and it still doesn't work. There are parts in the multiline lines string that i want to be variables so i need to stop the quotes, then start them agian.
  12. #7
  13. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    What you want is string formatting; although this could also be done using simple string concatenation. I would stick with string formatting for this one

    Code:
    >>> var1 = 'someValue'
    >>> var2 = 0
    >>> var3 = 1.5
    >>> 
    >>> 'var1 = %s; var2 = %i; var3 = %f' % (var1, var2, var3)
    'var1 = someValue; var2 = 0; var3 = 1.500000'
    >>> 'var1 = %s; var2 = %i; var3 = %0.2f' % (var1, var2, var3)
    'var1 = someValue; var2 = 0; var3 = 1.50'
    >>> 'var1 = %s; var2 = %s; var3 = %s' % (var1, var2, var3)
    'var1 = someValue; var2 = 0; var3 = 1.5'
    >>> '''The same thing over multiple lines:
    var1 = %s
    var2 = %i
    var3 = %f'''
    'The same thing over multiple lines:\nvar1 = %s\nvar2 = %i\nvar3 = %f'
    >>> print '''The same thing over multiple lines:
    var1 = %s
    var2 = %i
    var3 = %f'''
    The same thing over multiple lines:
    var1 = %s
    var2 = %i
    var3 = %f
    >>>
    If you've ever used the C style printf() in other languages then you'll probably be fine with string formatting already, but its worth a read just to get to grips with how Python handles it .

    You can read more about Python strings and string formatting here:

    http://www.python.org/doc/2.4/tut/node5.html#SECTION005120000000000000000
    http://www.python.org/doc/2.4/lib/typesseq-strings.html

    Mark.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo