Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    23
    Rep Power
    0

    Question shutil copyfile: how come it truncates data?


    I'm not sure what's going on but the copy I'm using below doesn't really copy. It only copies up to 196 lines the rest it leaves off.

    Copied file:
    Lines: 196
    File size: 12483 bytes
    Size on disk: 16,384 bytes

    Original file:
    Lines: 247
    File size: 13499 bytes
    Size on disk: 16,384 bytes

    Code:
    shutil.copyfile('file1.html', 'file2.html')
    I'm using python 2.3 on win2k. Did I miss something here?

    Any help would be greatly appreciated!

    -Rick

    EDIT:
    Code:
    infile = open('file1.html', "rb")
        outfile = open('file2.html', "wb")
        outfile.write(infile.read())
    The above code snippet yeilds the same truncated results... what gives?
    Last edited by rickt; February 5th, 2004 at 02:23 PM.
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    If you can attach the file for testing perposes it would probably help alot , of even if you provide a URL or emailed it to me...

    netytan at hormail dot come

    as far as i can see theres no reason why this should be happening so hopefully we can figure this out.

    Note: you should really close() file instances or use the

    var = file('name', 'mode').method()

    format; where the mode is r, w, a etc and the method is read() or write() i.e. your code in a simple line (doesnt require close())

    Code:
    >>> file('file2.txt', 'w').write(file('file1.txt', 'r').read())
    Mark.
    programming language development: www.netytan.com Hula

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

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    13
    netytan: creating a file object and using methods on it in one line is no different than creating it and then using its methods on a separate line. Closing it is unimportant too. As long as all references to it go away, it will be closed and summarily gc'ed. Writing big-*** one-liners like that just detract from readability.
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Although contrary to what i have read/heard! Infact i believe this (closing file instances) came form Learning Python which admitadly this is pretty old

    And yes i am aware that the garbage colector will remove unneeded objects

    Finally , i dont see anything wrong with one liners in certain cases...

    >>> file('file.txt', 'r').read()

    looks cleaner to me than

    >>> name = file('file.txt', 'r')
    >>> name.read()
    >>> name.close()

    Mark.
    Last edited by netytan; February 5th, 2004 at 06:16 PM.
    programming language development: www.netytan.com Hula

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

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    13
    I agree that that one-liner is fine, but nesting that inside another similar one is probably too much.

    A 59-character line of python with only a few characters devoted to literals and with no function name longer than 5 letters is generally too long.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    325
    Rep Power
    12
    It's just bad practice not to close a file when you're finished using it. You will be using other languages at some stage, and this bad habbit will likely spill over to there.

    I'd probably go with this. It's readable and fairly short and closes all handles.
    Not that I've checked that it works :P

    Code:
    infile = file('file1.txt', 'r').read()
    outfile = file('file2.txt', 'w').write(infile)
    infile.close()
    outfile.close()

    [EDIT] Ok .. that didnt work =P
    hmmm ... I'd possibly go with the first 2 lines due to consiceness and readability. Tho then Iv'e contradicted myself completely from before ;P[/EDIT]
    Last edited by lazy_yogi; February 5th, 2004 at 07:42 PM.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    13
    Well, in python I believe you pretty much never see people explicitly closing file handles (or del'ing them) because it's understood that they will be closed when they are gc'ed. That's part of the beauty of high-level languages, you don't have to worry about tasks like that. Much like you don't have to worry about freeing memory for every time you allocate it, you don't have to worry about closing a file handle every time you open one.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    325
    Rep Power
    12
    You have to explicitely close a connection to the database every time or mysql locks up.

    And this would be a serious problem with commercial software.

    Taking care of memory is a well konwn issue with high level languages, but you can't assume freeing all types of resources isn't automatically handled.

    Tho it's nice that python handles this.
  16. #9
  17. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Now that i think about it - that being it isnt 1am - surly lines like this one...

    >>> file('file.txt', 'r').read()

    are caught by the grabage collector nie imediatly because there is no referance to the file anywhere. Its simply created, used and lost. where in this example the referance is there longer (and if you dont close() it untill the grabage collector removes it).

    >>> name = file('file.txt', 'r')
    >>> name.read()
    >>> name.close()

    seems logical?

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

  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    13
    Yeah, if all you are going to do to the file is just read the data in, then doing:
    Code:
    data = file('foo', 'r').read()
    is fine. I just wouldn't try to accomplish more than one task on the same line like was being done with the line where the data was read from one file and written to another file at once.
  20. #11
  21. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Got ya
    programming language development: www.netytan.com Hula

  22. #12
  23. Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    23
    Rep Power
    0

    Post Python Script


    Originally posted by netytan
    If you can attach the file for testing perposes it would probably help alot
    First off, thanks for the suggestions. Second, I'm going to go out on a limb pull a little SYA and past my code here. Try not to poke too much fun. I'm new to this.

    Code:
    import urllib
    import httplib
    import filecmp
    import os
    import shutil
    
    params = urllib.urlencode({"R":"yes","D":"MH"})
    h = httplib.HTTP("securityresponse.symantec.com:80")
    h.putrequest("POST", "/avcenter/cgi-bin/updates_msa.cgi")
    h.putheader("Content-length", "%d" % len(params))
    h.putheader('Accept', 'text/plain')
    h.putheader('Host', 'securityresponse.symantec.com')
    h.endheaders()
    h.send(params)
    reply, msg, hdrs = h.getreply()
    print reply 
    data = h.getfile().read()
    file_name = "NIDS.html"
    the_file = open(file_name,'w')
    the_file.write(data)
    print "Grabbed the HTML and wrote the file."
    
    old_file_name = "NIDS_Original.html"
    return_info = filecmp.cmp (file_name, old_file_name)
    # print return_info
    
    if return_info == False:
        desktop_file_name = 'c:\\documents and settings\\all users\\desktop\\ALERT Symantec Page Changed.html'
        the_file1 = open(desktop_file_name, 'w')
        the_file1.write(data)
        shutil.copyfile('NIDS.html', 'c:\\documents and settings\\all users\\desktop\\ALERT Symantec Page Changed.html')
        infile = open('NIDS.html', "rb")
        outfile = open('c:\\documents and settings\\all users\\desktop\\ALERT Symantec Page Changed.html', "wb")
        shutil.copy('NIDS.html', 'NIDS_Original.html')
        print 'FALSE:  Copied NIDS.html to ALERT on the desktop.'
    print 'TRUE:  Deleted the temp file NIDS.html.'
    os.remove('NIDS.html')
    Ok, one thing I know I need to change is I need to get into the habbit of throwing in some comments. I'll be sure to touch up my code right now. Let me know if I should post the commented version. You guys will probably all think this is really basic stuff though. <shrug>
  24. #13
  25. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Naw i've seen worse believe me , its actually pretty neat! But really needing a sample file to test it with. are you just using the symantec website?

    Edit: Never mind i got it, just renamed the file created by you're program, since NIDS_Original.html sisnt exist. I'll give it a go now.

    Mark.
    Last edited by netytan; February 6th, 2004 at 10:10 AM.
    programming language development: www.netytan.com Hula

  26. #14
  27. Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    23
    Rep Power
    0

    More info


    Thanks for looking at it. The bottom line that I'm looking for is:

    Go to that web site and check if there's a new update for the NIDS. Then, if there is do something. The something so far is create an HTML page on the desktop with the relevant content.

    I didn't do any error checking in my program because I'm not really sure how to do that.

    FYI: I've read almost all of the tutorials that I could find. But I'm better at learning things hands on so I figured I'd start out writing a program that I actually needed. I learn better that way and this is something I need so I can't just forget about it and move on to something else.
  28. #15
  29. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Ok, so whats the actual page you're trying to get to and retrive? For some reason you're page seems to be copied to the desktop in binary mode :S. Looking into it.

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

Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo