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

    Join Date
    Mar 2013
    Posts
    12
    Rep Power
    0

    Writelines() anomaly or writelines() facepalm?


    So, this part of my code works great:

    PHP Code:
    with open('temp/001.tle''w+') as temp001:
        
    temp001.writelines([tle1.line1tle1.line2])
    with open('temp/002.tle''w+') as temp002:
        
    temp002.writelines([tle2.line1tle2.line2])
    with open('temp/003.tle''w+') as temp003:
        
    temp003.writelines([tle3.line1tle3.line2]) 
    The files 001.tle, 002.tle, and 003.tle all contain the appropriate data now. However, it is immediately followed by this:

    PHP Code:
    with open('temp/base.tle''w+') as base:
        
    tle1.line1 base.readline()
        
    tle1.line2 base.readline() 
    Not only does this change tle1.line and tle1.line2, but it goes back and changes the lines of 001.tle that were written with tle1.line1 and tle1.line2.

    I can only assume that writelines is working with the pointers of the objects passed in and not the objects themselves, and then updating the file when the objects at those pointers change. However, I cannot find any documentation/report on this AT ALL. Even still, the file 000.tle should be CLOSED before the lines are changed, so this connection shouldn't exist at that point anyway, right?

    So, I'm guessing I need to find a way to deep copy the strings I want to pass in?

    Please, make me face palm hard at this.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,855
    Rep Power
    481
    You opened base.tle with mode 'w+' which means
    in python3
    Code:
    Character	Meaning
    'r'	open for reading (default)
    'w'	open for writing, truncating the file first
    'x'	open for exclusive creation, failing if the file already exists
    'a'	open for writing, appending to the end of the file if it exists
    'b'	binary mode
    't'	text mode (default)
    '+'	open a disk file for updating (reading and writing)
    'U'	universal newlines mode (for backwards compatibility; should not be used in new code)

    For me, on unix, a complete program
    Code:
    class c:
        pass
    
    tle1 = c()
    tle1.line1 = 'tle1.line1 = '
    tle1.line2 = 'tle1.line2 = '
    tle2 = c()
    tle2.line1 = 'tle2.line1 = '
    tle2.line2 = 'tle2.line2 = '
    tle3 = c()
    tle3.line1 = 'tle3.line1 = '
    tle3.line2 = 'tle3.line2 = '
    
    with open('/tmp/base.tle', 'w') as base:
        base.write('blort\nghoul\n')
    with open('/tmp/001.tle', 'w+') as temp001:
        temp001.writelines([tle1.line1, tle1.line2])
    with open('/tmp/002.tle', 'w+') as temp002:
        temp002.writelines([tle2.line1, tle2.line2])
    with open('/tmp/003.tle', 'w+') as temp003:
        temp003.writelines([tle3.line1, tle3.line2])
    with open('/tmp/base.tle', 'w+') as base:
        tle1.line1 = base.readline()
        tle1.line2 = base.readline()
    print(tle1.line1)
    print(tle1.line2)
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    12
    Rep Power
    0
    That definitely is an issue I will fix, but I was wondering why reading lines from base.tle changes my 001.tle file...any ideas? And more importantly, how to get around this problem?
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,855
    Rep Power
    481
    I use a ubuntu python distributions. After running the program I verified the content of all 4 files. They are all as I expect. Are you running python within an operating system that you wrote?
    Code:
    $ for f in *.tle ; do echo '$ cat '$f ; cat $f ; done
    $ cat 001.tle
    tle1.line1 = tle1.line2 = $ cat 002.tle
    tle2.line1 = tle2.line2 = $ cat 003.tle
    tle3.line1 = tle3.line2 = $ cat base.tle
    Hard to read because the files haven't got new lines. base.tle is empty. The other files contain the correct content.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    12
    Rep Power
    0
    I made a workaround and this isn't really an issue for me anymore, but it is interesting. I'm running py2.7 on win xp (32 bit)

IMN logo majestic logo threadwatch logo seochat tools logo