<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>遨游世界 &#187; 简明python教程</title>
	<atom:link href="http://www.liujianfei.com/blog/archives/tag/%e7%ae%80%e6%98%8epython%e6%95%99%e7%a8%8b/feed" rel="self" type="application/rss+xml" />
	<link>http://www.liujianfei.com/blog</link>
	<description></description>
	<lastBuildDate>Wed, 08 Feb 2012 05:23:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>简明python教程</title>
		<link>http://www.liujianfei.com/blog/archives/96</link>
		<comments>http://www.liujianfei.com/blog/archives/96#comments</comments>
		<pubDate>Tue, 22 Dec 2009 02:09:40 +0000</pubDate>
		<dc:creator>blackcat</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[简明python教程]]></category>

		<guid isPermaLink="false">http://www.liujianfei.com/blog/?p=96</guid>
		<description><![CDATA[在例10.4 备份脚本——版本四的代码中有些需要注意的地方。 #!/usr/bin/python # Filename: backup_ver4.py import os import time # 1. The files and directories to be backed up are specified in a list. source = &#91;&#8216;/home/swaroop/byte&#8217;, &#8216;/home/swaroop/bin&#8217;&#93; # If you are using Windows, use source = [r'C:\Documents', r'D:\Work'] or something like that # 2. The backup must be stored in a main backup [...]]]></description>
			<content:encoded><![CDATA[<p>在例10.4 备份脚本——版本四的代码中有些需要注意的地方。</p>
<div class="codesnip-container" >
<div class="python codesnip" style="font-family:monospace;"><span class="co1">#!/usr/bin/python</span><br />
<span class="co1"># Filename: backup_ver4.py</span><br />
<span class="kw1">import</span> <span class="kw3">os</span><br />
<span class="kw1">import</span> <span class="kw3">time</span><br />
<span class="co1"># 1. The files and directories to be backed up are specified in a list.</span><br />
source = <span class="br0">&#91;</span><span class="st0">&#8216;/home/swaroop/byte&#8217;</span>, <span class="st0">&#8216;/home/swaroop/bin&#8217;</span><span class="br0">&#93;</span><br />
<span class="co1"># If you are using Windows, use source = [r'C:\Documents', r'D:\Work'] or something like that</span><br />
<span class="co1"># 2. The backup must be stored in a main backup directory</span><br />
target_dir = <span class="st0">&#8216;/mnt/e/backup/&#8217;</span> <span class="co1"># Remember to change this to what you will be using</span><br />
<span class="co1"># 3. The files are backed up into a zip file.</span><br />
<span class="co1"># 4. The current day is the name of the subdirectory in the main directory</span><br />
today = target_dir + <span class="kw3">time</span>.<span class="me1">strftime</span><span class="br0">&#40;</span><span class="st0">&#8216;%Y%m%d&#8217;</span><span class="br0">&#41;</span><br />
<span class="co1"># The current time is the name of the zip archive</span><br />
now = <span class="kw3">time</span>.<span class="me1">strftime</span><span class="br0">&#40;</span><span class="st0">&#8216;%H%M%S&#8217;</span><span class="br0">&#41;</span><br />
<span class="co1"># Take a comment from the user to create the name of the zip file</span><br />
comment = <span class="kw2">raw_input</span><span class="br0">&#40;</span><span class="st0">&#8216;Enter a comment &#8211;&amp;gt; &#8216;</span><span class="br0">&#41;</span><br />
<span class="kw1">if</span> <span class="kw2">len</span><span class="br0">&#40;</span>comment<span class="br0">&#41;</span> == <span class="nu0">0</span>: <span class="co1"># check if a comment was entered</span><br />
target = today + <span class="kw3">os</span>.<span class="me1">sep</span> + now + <span class="st0">&#8216;.zip&#8217;</span><br />
<span class="kw1">else</span>:<br />
target = today + <span class="kw3">os</span>.<span class="me1">sep</span> + now + <span class="st0">&#8216;_&#8217;</span> + \<br />
comment.<span class="me1">replace</span><span class="br0">&#40;</span><span class="st0">&#8216; &#8216;</span>, <span class="st0">&#8216;_&#8217;</span><span class="br0">&#41;</span> + <span class="st0">&#8216;.zip&#8217;</span><br />
<span class="co1"># Notice the backslash!</span><br />
<span class="co1"># Create the subdirectory if it isn&#8217;t already there</span><br />
<span class="kw1">if</span> <span class="kw1">not</span> <span class="kw3">os</span>.<span class="me1">path</span>.<span class="me1">exists</span><span class="br0">&#40;</span>today<span class="br0">&#41;</span>:<br />
<span class="kw3">os</span>.<span class="me1">mkdir</span><span class="br0">&#40;</span>today<span class="br0">&#41;</span> <span class="co1"># make directory</span><br />
<span class="kw1">print</span> <span class="st0">&#8216;Successfully created directory&#8217;</span>, today<br />
<span class="co1"># 5. We use the zip command (in Unix/Linux) to put the files in a zip archive</span><br />
zip_command = <span class="st0">&quot;zip -qr &#8216;%s&#8217; %s&quot;</span> <span class="sy0">%</span> <span class="br0">&#40;</span>target, <span class="st0">&#8216; &#8216;</span>.<span class="me1">join</span><span class="br0">&#40;</span>source<span class="br0">&#41;</span><span class="br0">&#41;</span><br />
<span class="co1"># Run the backup</span><br />
<span class="kw1">if</span> <span class="kw3">os</span>.<span class="me1">system</span><span class="br0">&#40;</span>zip_command<span class="br0">&#41;</span> == 0:<br />
<span class="kw1">print</span> <span class="st0">&#8216;Successful backup to&#8217;</span>, target<br />
<span class="kw1">else</span>:<br />
<span class="kw1">print</span> <span class="st0">&#8216;Backup FAILED&#8217;</span></div>
</div>
<p>文中提到“我还希望有的一个优化是使用tar命令替代zip命令。这样做的一个优势是在你结合使用tar和gzip<br />
命令的时候，备份会更快更小。如果你想要在Windows中使用这些归档，WinZip也能方便地处<br />
理这些.tar.gz文件。tar命令在大多数Linux/Unix系统中都是默认可用的。Windows用户也可以<a href="http://gnuwin32.sourceforge.net/packages/libarchive.htm">下<br />
载安装它</a>。<br />
命令字符串现在将成为：<br />
tar = &#8216;tar -cvzf %s %s -X /home/swaroop/excludes.txt&#8217; % (target, &#8216; &#8216;.join(srcdir))</p>
<p>需要补充的是Windows用户安装完LibArchive后命令为bsdtar，而且若想使用-z这个选项，还需安装gzip软件。</p>
<p>代码中的</p>
<div class="codesnip-container" >
<div class="python codesnip" style="font-family:monospace;">target = today + <span class="kw3">os</span>.<span class="me1">sep</span> + now + <span class="st0">&#8216;.zip&#8217;</span></div>
</div>
<p>改为</p>
<div class="codesnip-container" >
<div class="python codesnip" style="font-family:monospace;">target = today + <span class="kw3">os</span>.<span class="me1">sep</span> + now + <span class="st0">&#8216;.tar.gz&#8217;</span></div>
</div>
<p>使用bsdtar -cvzf %s %s -X /home/swaroop/excludes.txt&#8217; % (target, &#8216; &#8216;.join(source))</p>
<p>英文原版已经没有用tar替换zip的内容了，提出建议用Python标准库中zipfile和tarfile来重写ver5。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.liujianfei.com/blog/archives/96/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

