<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Tahoma
}
--></style>
</head>
<body class='hmmessage'><div dir='ltr'>
<div><br>

<meta http-equiv="Content-Type" content="text/html; charset=unicode">
<meta name="Generator" content="Microsoft SafeHTML">
<style>
.ExternalClass .ecxhmmessage P
{padding:0px;}
.ExternalClass body.ecxhmmessage
{font-size:10pt;font-family:Tahoma;}

</style>

<div dir="ltr">
Hi all</div><div dir="ltr"><br></div><div dir="ltr">&nbsp;I am new to programming and on a very steep curve. I am using Python 3 to learn and I am having trouble understanding exactly what pickling is.&nbsp;<div>I have written a program that asks for a URL and then writes the source code from the URL to a file, then pickles the file I just created.&nbsp;</div><div>I am not sure why I pickle something as opposed to just saving it as a normal file?</div><div><br></div><div>I saved the url source code file as a .txt and also the pickled file as .txt. I am not sure of the filetype I should save a pickled file as.</div><div>When I look at the files (the saved source file and the pickled file) they look the same. I am viewing them in notepad on the Mac.</div><div><br></div><div>Many thanks</div><div>Joe</div><div><br></div><div><div>###################################################################</div><div># &nbsp;Pickle an object- Puzzle 5&nbsp;</div><div># &nbsp;Joe Batt 18 Nov 2011 - http://www.pythonchallenge.com/pc/def/peak.html</div><div>###################################################################</div><div><br></div><div>import pickle</div><div>import urllib</div><div><br></div><div>def geturlfile(urlfile):</div><div>#gets the user URL from main opens it and saves it to var fileurl</div><div>&nbsp; &nbsp; from urllib.request import urlopen</div><div>&nbsp; &nbsp; fileurl=urlopen(urlfile)</div><div>#opens the file on computer and writes the var fileurl to it</div><div>&nbsp; &nbsp; html_file=open('///Users/joebatt/Desktop/python/puzzle5.txt','w')</div><div>&nbsp; &nbsp; for line in fileurl.readlines():</div><div>&nbsp; &nbsp; &nbsp; &nbsp; linestring=line.decode("utf-8") #ensures written in string not byte</div><div>&nbsp; &nbsp; &nbsp; &nbsp; html_file.write(linestring)</div><div>&nbsp; &nbsp; html_file.close() #closes the puzzle file</div><div>#just prints file to check its correct</div><div>&nbsp; &nbsp; html_file=open('///Users/joebatt/Desktop/python/puzzle5.txt','r')</div><div>&nbsp; &nbsp; filecontents=html_file.read()</div><div>&nbsp; &nbsp; html_file.close()</div><div>&nbsp; &nbsp; print (filecontents)</div><div>&nbsp; &nbsp; print (type(filecontents))</div><div><br></div><div>#pickles the file filecontents containing the url file</div><div>&nbsp; &nbsp; pickledfile=open('///Users/joebatt/Desktop/python/pickledpuzzle5.txt','wb')</div><div>&nbsp; &nbsp; pickle.dump(filecontents,pickledfile)</div><div>&nbsp; &nbsp; pickledfile.close() &nbsp; &nbsp; &nbsp;&nbsp;</div><div>&nbsp; &nbsp; return ()</div><div><br></div><div>&nbsp; &nbsp;&nbsp;</div><div><br></div><div># Main program</div><div>urlfile=input('Please input the URL ')</div><div>geturlfile(urlfile)</div></div>                                               </div></div>                                               </div></body>
</html>