﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>IT博客-yunshichen-随笔分类-Python3</title><link>http://www.cnitblog.com/yunshichen/category/8168.html</link><description>我相信人生是值得活的,尽管人在一生中必须遭受痛苦,卑劣,残酷,不幸和死亡的折磨,我依然深信如此.但我认为人生不一定要有意义,只是对一些人而言,他们可以使人生有意义.  ---J 赫胥黎</description><language>zh-cn</language><lastBuildDate>Mon, 26 Sep 2011 06:18:37 GMT</lastBuildDate><pubDate>Mon, 26 Sep 2011 06:18:37 GMT</pubDate><ttl>60</ttl><item><title>Python 3 教程二:文件,目录和路径</title><link>http://www.cnitblog.com/yunshichen/archive/2009/04/01/55931.html</link><dc:creator>Chenyunshi</dc:creator><author>Chenyunshi</author><pubDate>Wed, 01 Apr 2009 08:22:00 GMT</pubDate><guid>http://www.cnitblog.com/yunshichen/archive/2009/04/01/55931.html</guid><wfw:comment>http://www.cnitblog.com/yunshichen/comments/55931.html</wfw:comment><comments>http://www.cnitblog.com/yunshichen/archive/2009/04/01/55931.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cnitblog.com/yunshichen/comments/commentRss/55931.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/yunshichen/services/trackbacks/55931.html</trackback:ping><description><![CDATA[
		<br />
		<h3>
				<b>
						<font color="#006600">1 遍历文件夹和文件</font>
				</b>
		</h3>
		<b>
				<br />
		</b>
		<br />
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
				<span style="color: rgb(0, 0, 255);">import</span>
				<span style="color: rgb(0, 0, 0);"> os<br /></span>
				<span style="color: rgb(0, 0, 255);">import</span>
				<span style="color: rgb(0, 0, 0);"> os.path<br /></span>
				<span style="color: rgb(0, 128, 0);">#</span>
				<span style="color: rgb(0, 128, 0);"> os,os.path里包含大多数文件访问的函数,所以要先引入它们.</span>
				<span style="color: rgb(0, 128, 0);">
						<br />#</span>
				<span style="color: rgb(0, 128, 0);"> 请按照你的实际情况修改这个路径</span>
				<span style="color: rgb(0, 128, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 0);">rootdir </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(128, 0, 0);">d:/download</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 255);">for</span>
				<span style="color: rgb(0, 0, 0);"> parent, dirnames, filenames </span>
				<span style="color: rgb(0, 0, 255);">in</span>
				<span style="color: rgb(0, 0, 0);"> os.walk(rootdir):<br />    </span>
				<span style="color: rgb(0, 128, 0);">#</span>
				<span style="color: rgb(0, 128, 0);">case 1:</span>
				<span style="color: rgb(0, 128, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 0);">    </span>
				<span style="color: rgb(0, 0, 255);">for</span>
				<span style="color: rgb(0, 0, 0);"> dirname </span>
				<span style="color: rgb(0, 0, 255);">in</span>
				<span style="color: rgb(0, 0, 0);"> dirnames:<br />        </span>
				<span style="color: rgb(0, 0, 255);">print</span>
				<span style="color: rgb(0, 0, 0);"> (</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(128, 0, 0);">parent is:</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 0);">+</span>
				<span style="color: rgb(0, 0, 0);"> parent)<br />        </span>
				<span style="color: rgb(0, 0, 255);">print</span>
				<span style="color: rgb(0, 0, 0);"> (</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(128, 0, 0);">dirname is:</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 0);">+</span>
				<span style="color: rgb(0, 0, 0);"> dirname)<br />    </span>
				<span style="color: rgb(0, 128, 0);">#</span>
				<span style="color: rgb(0, 128, 0);">case 2</span>
				<span style="color: rgb(0, 128, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 0);">    </span>
				<span style="color: rgb(0, 0, 255);">for</span>
				<span style="color: rgb(0, 0, 0);"> filename </span>
				<span style="color: rgb(0, 0, 255);">in</span>
				<span style="color: rgb(0, 0, 0);"> filenames:<br />        </span>
				<span style="color: rgb(0, 0, 255);">print</span>
				<span style="color: rgb(0, 0, 0);"> (</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(128, 0, 0);">parent is:</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 0);">+</span>
				<span style="color: rgb(0, 0, 0);"> parent)<br />        </span>
				<span style="color: rgb(0, 0, 255);">print</span>
				<span style="color: rgb(0, 0, 0);"> (</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(128, 0, 0);">filename with full path :</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 0);">+</span>
				<span style="color: rgb(0, 0, 0);"> os.path.join(parent, filename))<br /><br /></span>
				<span style="color: rgb(128, 0, 0);">'''</span>
				<span style="color: rgb(128, 0, 0);">知识点:<br /><br />    * os.walk返回一个三元组.其中dirnames是所有文件夹名字(不包含路径),filenames是所有文件的名字(不包含路径).parent表示父目录.<br />    * case1 演示了如何遍历所有目录.<br />    * case2 演示了如何遍历所有文件.<br />    * os.path.join(dirname,filename) : 将形如"/a/b/c"和"d.java"变成/a/b/c/d.java".<br /></span>
				<span style="color: rgb(128, 0, 0);">'''</span>
				<span style="color: rgb(0, 0, 0);">
						<br />
				</span>
		</div>
		<br />
		<p>
		</p>
		<h3>
				<b>
						<font>
								<b>
										<font color="#006600">2 分割路径和文件名</font>
								</b>
						</font>
				</b>
		</h3>
		<p>
		</p>
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
				<span style="color: rgb(0, 0, 255);">import</span>
				<span style="color: rgb(0, 0, 0);"> os.path<br /></span>
				<span style="color: rgb(0, 128, 0);">#</span>
				<span style="color: rgb(0, 128, 0);">常用函数有三种:分隔路径,找出文件名.找出盘符(windows系统),找出文件的扩展名.</span>
				<span style="color: rgb(0, 128, 0);">
						<br />#</span>
				<span style="color: rgb(0, 128, 0);">根据你机器的实际情况修改下面参数.</span>
				<span style="color: rgb(0, 128, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 0);">spath</span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(128, 0, 0);">D:/download/repository.7z</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">
						<br />
						<br />
				</span>
				<span style="color: rgb(0, 128, 0);">#</span>
				<span style="color: rgb(0, 128, 0);"> case 1:</span>
				<span style="color: rgb(0, 128, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 0);">p,f</span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);">os.path.split(spath);<br /></span>
				<span style="color: rgb(0, 0, 255);">print</span>
				<span style="color: rgb(0, 0, 0);">(</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(128, 0, 0);">dir is:</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">+</span>
				<span style="color: rgb(0, 0, 0);">p)<br /></span>
				<span style="color: rgb(0, 0, 255);">print</span>
				<span style="color: rgb(0, 0, 0);">(</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(128, 0, 0);">file is:</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">+</span>
				<span style="color: rgb(0, 0, 0);">f)<br /><br /></span>
				<span style="color: rgb(0, 128, 0);">#</span>
				<span style="color: rgb(0, 128, 0);"> case 2:</span>
				<span style="color: rgb(0, 128, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 0);">drv,left</span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);">os.path.splitdrive(spath);<br /></span>
				<span style="color: rgb(0, 0, 255);">print</span>
				<span style="color: rgb(0, 0, 0);">(</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(128, 0, 0);">driver is:</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">+</span>
				<span style="color: rgb(0, 0, 0);">drv)<br /></span>
				<span style="color: rgb(0, 0, 255);">print</span>
				<span style="color: rgb(0, 0, 0);">(</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(128, 0, 0);">left is:</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">+</span>
				<span style="color: rgb(0, 0, 0);">left)<br /></span>
				<span style="color: rgb(0, 128, 0);">#</span>
				<span style="color: rgb(0, 128, 0);"> case 3:</span>
				<span style="color: rgb(0, 128, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 0);">f,ext</span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);">os.path.splitext(spath);<br /></span>
				<span style="color: rgb(0, 0, 255);">print</span>
				<span style="color: rgb(0, 0, 0);">(</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(128, 0, 0);">f is:</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">+</span>
				<span style="color: rgb(0, 0, 0);">f)<br /></span>
				<span style="color: rgb(0, 0, 255);">print</span>
				<span style="color: rgb(0, 0, 0);">(</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(128, 0, 0);">ext is:</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">+</span>
				<span style="color: rgb(0, 0, 0);">ext)<br /></span>
				<span style="color: rgb(128, 0, 0);">'''</span>
				<span style="color: rgb(128, 0, 0);">
						<br />    知识点:    这三个函数都返回二元组.<br />    * case1 分隔目录和文件名<br />    * case2 分隔盘符和文件名<br />    * case3 分隔文件和扩展名<br /></span>
				<span style="color: rgb(128, 0, 0);">'''</span>
				<span style="color: rgb(0, 0, 0);">
						<br />
				</span>
		</div>
		<br />
		<br />
		<b>总结:5个函数</b>
		<br />
		<ul>
				<li>os.walk(spath)</li>
				<li>os.path.split(spath)</li>
				<li>os.path.splitdrive(spath)</li>
				<li>os.path.splitext(spath)</li>
				<li>os.path.join(path1,path2)</li>
		</ul>
		<br />
		<h3>
				<b>
						<font>
								<b>
										<font color="#006600">3 复制文件</font>
								</b>
						</font>
				</b>
		</h3>
		<br />
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
				<span style="color: rgb(0, 0, 255);">import</span>
				<span style="color: rgb(0, 0, 0);"> shutil<br /></span>
				<span style="color: rgb(0, 0, 255);">import</span>
				<span style="color: rgb(0, 0, 0);"> os<br /></span>
				<span style="color: rgb(0, 0, 255);">import</span>
				<span style="color: rgb(0, 0, 0);"> os.path<br /><br />src</span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(128, 0, 0);">d:\\download\\test\\myfile1.txt</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">
						<br />dst</span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(128, 0, 0);">d:\\download\\test\\myfile2.txt</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">
						<br />dst2</span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(128, 0, 0);">d:/download/test/测试文件夹.txt</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">
						<br />
						<br />dir1</span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);">os.path.dirname(src)<br /><br /></span>
				<span style="color: rgb(0, 0, 255);">print</span>
				<span style="color: rgb(0, 0, 0);">(</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(128, 0, 0);">dir1 %s</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">%</span>
				<span style="color: rgb(0, 0, 0);">dir1)<br /><br /></span>
				<span style="color: rgb(0, 0, 255);">if</span>
				<span style="color: rgb(0, 0, 0);">(os.path.exists(src)</span>
				<span style="color: rgb(0, 0, 0);">==</span>
				<span style="color: rgb(0, 0, 0);">False):<br />    os.makedirs(dir1)       <br /><br />f1</span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);">open(src,</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(128, 0, 0);">w</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">)<br />f1.write(</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(128, 0, 0);">line a\n</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">)<br />f1.write(</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(128, 0, 0);">line b\n</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">)<br />f1.close()<br /><br /><br />shutil.copyfile(src, dst)<br />shutil.copyfile(src, dst2)<br />f2</span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);">open(dst,</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(128, 0, 0);">r</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">)<br /></span>
				<span style="color: rgb(0, 0, 255);">for</span>
				<span style="color: rgb(0, 0, 0);"> line </span>
				<span style="color: rgb(0, 0, 255);">in</span>
				<span style="color: rgb(0, 0, 0);"> f2:<br />    </span>
				<span style="color: rgb(0, 0, 255);">print</span>
				<span style="color: rgb(0, 0, 0);">(line)<br /><br />f2.close()<br /><br /></span>
				<span style="color: rgb(0, 128, 0);">#</span>
				<span style="color: rgb(0, 128, 0);">测试复制文件夹树</span>
				<span style="color: rgb(0, 128, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 255);">try</span>
				<span style="color: rgb(0, 0, 0);">:<br />    srcDir</span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(128, 0, 0);">d:/download/test</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">
						<br />    dstDir</span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(128, 0, 0);">d:/download/test2</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">
						<br />    </span>
				<span style="color: rgb(0, 128, 0);">#</span>
				<span style="color: rgb(0, 128, 0);">如果dstDir已经存在,那么shutil.copytree方法会报错!</span>
				<span style="color: rgb(0, 128, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 0);">    </span>
				<span style="color: rgb(0, 128, 0);">#</span>
				<span style="color: rgb(0, 128, 0);">这也意味着你不能直接用d:作为目标路径.</span>
				<span style="color: rgb(0, 128, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 0);">    shutil.copytree(srcDir, dstDir)<br /></span>
				<span style="color: rgb(0, 0, 255);">except</span>
				<span style="color: rgb(0, 0, 0);"> Exception as err:<br />    </span>
				<span style="color: rgb(0, 0, 255);">print</span>
				<span style="color: rgb(0, 0, 0);"> (err)<br />    <br /></span>
				<span style="color: rgb(128, 0, 0);">'''</span>
				<span style="color: rgb(128, 0, 0);">
						<br />    知识点:<br />    * shutil.copyfile:如何复制文件<br />    * os.path.exists:如何判断文件夹是否存在<br />    * shutil.copytree:如何复制目录树    <br /></span>
				<span style="color: rgb(128, 0, 0);">'''</span>
		</div>
		<br />
		<b>总结:4个函数</b>
		<br />
		<ul>
				<li>os.path.dirname(path)</li>
				<li>os.path.exists(path)</li>
				<li>shutil.copyfile(src, dst)</li>
				<li>shutil.copytree(srcDir, dstDir)</li>
		</ul>
		<br />
		<p>
		</p>
		<h3>
				<b>
						<font>
								<b>
										<font color="#006600">4 实战:文件备份小程序</font>
								</b>
						</font>
				</b>
				<br />
		</h3>
		<br />
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
				<span style="color: rgb(0, 0, 255);">import</span>
				<span style="color: rgb(0, 0, 0);"> os<br /></span>
				<span style="color: rgb(0, 0, 255);">import</span>
				<span style="color: rgb(0, 0, 0);"> shutil<br /></span>
				<span style="color: rgb(0, 0, 255);">import</span>
				<span style="color: rgb(0, 0, 0);"> datetime<br /><br /></span>
				<span style="color: rgb(128, 0, 0);">'''</span>
				<span style="color: rgb(128, 0, 0);">
						<br />作用：将目录备份到其他路径。<br />实际效果：<br />假设给定目录"/media/data/programmer/project/python" ，<br />备份路径"/home/diegoyun/backup/“ ，<br />则会将python目录备份到备份路径下，形如：<br />/home/diegoyun/backup/yyyymmddHHMMSS/python/xxx/yyy/zzz<img src="http://www.cnitblog.com/images/dot.gif" />..<br /><br />用法:更改这两个参数.<br />backdir:备份目的地.<br />copydirs:想要备份的文件夹.<br /></span>
				<span style="color: rgb(128, 0, 0);">'''</span>
				<span style="color: rgb(0, 0, 0);">
						<br />
						<br />
						<br />
				</span>
				<span style="color: rgb(0, 0, 255);">def</span>
				<span style="color: rgb(0, 0, 0);"> mainLogic():<br />    </span>
				<span style="color: rgb(0, 128, 0);">#</span>
				<span style="color: rgb(0, 128, 0);">add dirs you want to copy</span>
				<span style="color: rgb(0, 128, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 0);">    backdir</span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(128, 0, 0);">d:\\test</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">
						<br />    </span>
				<span style="color: rgb(0, 0, 255);">print</span>
				<span style="color: rgb(0, 0, 0);">(backdir)<br /><br />    copydirs</span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);">[]<br />    copydirs.append(</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(128, 0, 0);">d:\\temp</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">);<br />    </span>
				<span style="color: rgb(0, 128, 0);">#</span>
				<span style="color: rgb(0, 128, 0);">copydirs.append("d:\\test");</span>
				<span style="color: rgb(0, 128, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 0);">    <br />    <br /><br />    </span>
				<span style="color: rgb(0, 0, 255);">print</span>
				<span style="color: rgb(0, 0, 0);">(</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(128, 0, 0);">Copying files  ===================</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">)<br />    start</span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);">datetime.datetime.now()<br /><br />    </span>
				<span style="color: rgb(0, 128, 0);">#</span>
				<span style="color: rgb(0, 128, 0);">gen a data folder for backup</span>
				<span style="color: rgb(0, 128, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 0);">    backdir</span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);">os.path.join(backdir,start.strftime(</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(128, 0, 0);">%Y-%m-%d</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">))<br />    </span>
				<span style="color: rgb(0, 128, 0);">#</span>
				<span style="color: rgb(0, 128, 0);">print("backdir is:"+backdir)</span>
				<span style="color: rgb(0, 128, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 0);">
						<br />    <br />    kc</span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);">0<br />    </span>
				<span style="color: rgb(0, 0, 255);">for</span>
				<span style="color: rgb(0, 0, 0);"> d </span>
				<span style="color: rgb(0, 0, 255);">in</span>
				<span style="color: rgb(0, 0, 0);"> copydirs:<br />        kc</span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);">kc</span>
				<span style="color: rgb(0, 0, 0);">+</span>
				<span style="color: rgb(0, 0, 0);">copyFiles(d,backdir)<br /><br />    end</span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);">datetime.datetime.now()<br />    </span>
				<span style="color: rgb(0, 0, 255);">print</span>
				<span style="color: rgb(0, 0, 0);">(</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(128, 0, 0);">Finished! ===================</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">)<br />    </span>
				<span style="color: rgb(0, 0, 255);">print</span>
				<span style="color: rgb(0, 0, 0);">(</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(128, 0, 0);">Total files : </span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 0);">+</span>
				<span style="color: rgb(0, 0, 0);"> str(kc) )<br />    </span>
				<span style="color: rgb(0, 0, 255);">print</span>
				<span style="color: rgb(0, 0, 0);">(</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(128, 0, 0);">Elapsed time : </span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 0);">+</span>
				<span style="color: rgb(0, 0, 0);"> str((end</span>
				<span style="color: rgb(0, 0, 0);">-</span>
				<span style="color: rgb(0, 0, 0);">start).seconds)</span>
				<span style="color: rgb(0, 0, 0);">+</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(128, 0, 0);"> seconds</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">)<br /><br /></span>
				<span style="color: rgb(0, 0, 255);">def</span>
				<span style="color: rgb(0, 0, 0);"> copyFiles(copydir,backdir):<br />    prefix</span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);">getPathPrefix(copydir)<br />    </span>
				<span style="color: rgb(0, 128, 0);">#</span>
				<span style="color: rgb(0, 128, 0);">print("prefix is:"+prefix )   </span>
				<span style="color: rgb(0, 128, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 0);">
						<br />    i</span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);">0<br />    </span>
				<span style="color: rgb(0, 0, 255);">for</span>
				<span style="color: rgb(0, 0, 0);"> dirpath,dirnames,filenames </span>
				<span style="color: rgb(0, 0, 255);">in</span>
				<span style="color: rgb(0, 0, 0);"> os.walk(copydir):<br />        </span>
				<span style="color: rgb(0, 0, 255);">for</span>
				<span style="color: rgb(0, 0, 0);"> name </span>
				<span style="color: rgb(0, 0, 255);">in</span>
				<span style="color: rgb(0, 0, 0);"> filenames:<br />            oldpath</span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);">os.path.join(dirpath,name)<br />            newpath</span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);">omitPrefix(dirpath,prefix)<br />            </span>
				<span style="color: rgb(0, 0, 255);">print</span>
				<span style="color: rgb(0, 0, 0);">(</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(128, 0, 0);">backdir is:</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">+</span>
				<span style="color: rgb(0, 0, 0);">backdir )           <br />            newpath</span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);">os.path.join(backdir,newpath)<br />            </span>
				<span style="color: rgb(0, 0, 255);">print</span>
				<span style="color: rgb(0, 0, 0);">(</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(128, 0, 0);">newpath is:</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">+</span>
				<span style="color: rgb(0, 0, 0);">newpath)<br /><br />            </span>
				<span style="color: rgb(0, 0, 255);">if</span>
				<span style="color: rgb(0, 0, 0);"> os.path.exists(newpath)</span>
				<span style="color: rgb(0, 0, 0);">!=</span>
				<span style="color: rgb(0, 0, 0);">True:<br />                os.makedirs(newpath)  <br />            newpath</span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);">os.path.join(newpath,name)<br />            </span>
				<span style="color: rgb(0, 0, 255);">print</span>
				<span style="color: rgb(0, 0, 0);">(</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(128, 0, 0);">From:</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">+</span>
				<span style="color: rgb(0, 0, 0);">oldpath</span>
				<span style="color: rgb(0, 0, 0);">+</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(128, 0, 0);"> to:</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">+</span>
				<span style="color: rgb(0, 0, 0);">newpath)<br />            shutil.copyfile(oldpath,newpath)<br />            i</span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);">i</span>
				<span style="color: rgb(0, 0, 0);">+</span>
				<span style="color: rgb(0, 0, 0);">1</span>
				<span style="color: rgb(0, 0, 0);">
						<br />    </span>
				<span style="color: rgb(0, 0, 255);">return</span>
				<span style="color: rgb(0, 0, 0);"> i    <br /><br /></span>
				<span style="color: rgb(0, 0, 255);">def</span>
				<span style="color: rgb(0, 0, 0);"> getPathPrefix(fullpath):<br />    </span>
				<span style="color: rgb(0, 128, 0);">#</span>
				<span style="color: rgb(0, 128, 0);">Giving /media/data/programmer/project/ , get the prefix</span>
				<span style="color: rgb(0, 128, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 0);">    </span>
				<span style="color: rgb(0, 128, 0);">#</span>
				<span style="color: rgb(0, 128, 0);">/media/data/programmer/</span>
				<span style="color: rgb(0, 128, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 0);">    l</span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);">fullpath.split(os.path.sep)<br />    </span>
				<span style="color: rgb(0, 128, 0);">#</span>
				<span style="color: rgb(0, 128, 0);">print(str(l[-1]=="")    </span>
				<span style="color: rgb(0, 128, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 0);">    </span>
				<span style="color: rgb(0, 0, 255);">if</span>
				<span style="color: rgb(0, 0, 0);"> l[</span>
				<span style="color: rgb(0, 0, 0);">-</span>
				<span style="color: rgb(0, 0, 0);">1</span>
				<span style="color: rgb(0, 0, 0);">]</span>
				<span style="color: rgb(0, 0, 0);">==</span>
				<span style="color: rgb(128, 0, 0);">""</span>
				<span style="color: rgb(0, 0, 0);">:<br />        tmp</span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);">l[</span>
				<span style="color: rgb(0, 0, 0);">-</span>
				<span style="color: rgb(0, 0, 0);">2</span>
				<span style="color: rgb(0, 0, 0);">]<br />    </span>
				<span style="color: rgb(0, 0, 255);">else</span>
				<span style="color: rgb(0, 0, 0);">:<br />        tmp</span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);">l[</span>
				<span style="color: rgb(0, 0, 0);">-</span>
				<span style="color: rgb(0, 0, 0);">1</span>
				<span style="color: rgb(0, 0, 0);">]<br />    </span>
				<span style="color: rgb(0, 0, 255);">return</span>
				<span style="color: rgb(0, 0, 0);"> fullpath[0:len(fullpath)</span>
				<span style="color: rgb(0, 0, 0);">-</span>
				<span style="color: rgb(0, 0, 0);">len(tmp)</span>
				<span style="color: rgb(0, 0, 0);">-</span>
				<span style="color: rgb(0, 0, 0);">1</span>
				<span style="color: rgb(0, 0, 0);">]<br /><br /></span>
				<span style="color: rgb(0, 0, 255);">def</span>
				<span style="color: rgb(0, 0, 0);"> omitPrefix(fullpath,prefix):<br />    </span>
				<span style="color: rgb(0, 128, 0);">#</span>
				<span style="color: rgb(0, 128, 0);">Giving /media/data/programmer/project/python/tutotial/file/test.py ,</span>
				<span style="color: rgb(0, 128, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 0);">    </span>
				<span style="color: rgb(0, 128, 0);">#</span>
				<span style="color: rgb(0, 128, 0);">and prefix is Giving /media/data/programmer/project/,</span>
				<span style="color: rgb(0, 128, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 0);">    </span>
				<span style="color: rgb(0, 128, 0);">#</span>
				<span style="color: rgb(0, 128, 0);">return path as python/tutotial/file/test.py</span>
				<span style="color: rgb(0, 128, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 0);">    </span>
				<span style="color: rgb(0, 0, 255);">return</span>
				<span style="color: rgb(0, 0, 0);"> fullpath[len(prefix)</span>
				<span style="color: rgb(0, 0, 0);">+</span>
				<span style="color: rgb(0, 0, 0);">1</span>
				<span style="color: rgb(0, 0, 0);">:]<br /><br />mainLogic()<br /></span>
		</div>
		<br />
		<h3>
				<b>
						<font>
								<b>
										<font color="#006600">5 资源:方法详解</font>
								</b>
						</font>
				</b>
		</h3>你可以在这里看到更多的api解释,感谢该作者: <a target="_blank" title="http://www.cnpython.org/docs/200/p_119.html" href="http://www.cnpython.org/docs/200/p_119.html">http://www.cnpython.org/docs/200/p_119.html</a><br /><br /><img src ="http://www.cnitblog.com/yunshichen/aggbug/55931.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/yunshichen/" target="_blank">Chenyunshi</a> 2009-04-01 16:22 <a href="http://www.cnitblog.com/yunshichen/archive/2009/04/01/55931.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Python 3 教程一:入门</title><link>http://www.cnitblog.com/yunshichen/archive/2009/04/01/55924.html</link><dc:creator>Chenyunshi</dc:creator><author>Chenyunshi</author><pubDate>Wed, 01 Apr 2009 04:08:00 GMT</pubDate><guid>http://www.cnitblog.com/yunshichen/archive/2009/04/01/55924.html</guid><wfw:comment>http://www.cnitblog.com/yunshichen/comments/55924.html</wfw:comment><comments>http://www.cnitblog.com/yunshichen/archive/2009/04/01/55924.html#Feedback</comments><slash:comments>14</slash:comments><wfw:commentRss>http://www.cnitblog.com/yunshichen/comments/commentRss/55924.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/yunshichen/services/trackbacks/55924.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: Python已经是3.1版本了,与时俱进更新教程.(由于Django不支持python3, 所以为了你的发展潜力, 建议你学习python2.x)python 2.x教程地址: http://www.cnitblog.com/yunshichen/archive/2008/05/09/43527.html本文适合有Java编程经验的程序员快速熟悉Python本文程序在windows xp+pyth...&nbsp;&nbsp;<a href='http://www.cnitblog.com/yunshichen/archive/2009/04/01/55924.html'>阅读全文</a><img src ="http://www.cnitblog.com/yunshichen/aggbug/55924.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/yunshichen/" target="_blank">Chenyunshi</a> 2009-04-01 12:08 <a href="http://www.cnitblog.com/yunshichen/archive/2009/04/01/55924.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>