|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
||||
|
||||
|
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
|
||||
|
||||
|
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. |
|
#3
|
|||
|
|||
|
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.
|
|
#4
|
||||
|
||||
|
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. |
|
#5
|
|||
|
|||
|
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. |
|
#6
|
|||
|
|||
|
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. |
|
#7
|
|||
|
|||
|
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.
|
|
#8
|
|||
|
|||
|
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. |
|
#9
|
||||
|
||||
|
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. |
|
#10
|
|||
|
|||
|
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. |
|
#11
|
||||
|
||||
|
Got ya
![]() |
|
#12
|
||||
|
||||
|
Quote:
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> |
|
#13
|
||||
|
||||
|
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. |
|
#14
|
||||
|
||||
|
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 somethi |