Docs/Python/python路径常用函数.html
2022-10-18 16:59:37 +08:00

165 lines
No EOL
8.1 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="tool" content="leanote-desktop-app">
<title>python路径常用函数</title>
<style>
*{font-family:"lucida grande","lucida sans unicode",lucida,helvetica,"Hiragino Sans GB","Microsoft YaHei","WenQuanYi Micro Hei",sans-serif;}
body {
margin: 0;
}
/*公用文字样式*/
h1{font-size:30px}h2{font-size:24px}h3{font-size:18px}h4{font-size:14px}
.note-container{
width:850px;
margin:auto;
padding: 10px 20px;
box-shadow: 1px 1px 10px #eee;
}
#title {
margin: 0;
}
table {
margin-bottom: 16px;
border-collapse: collapse;
}
table th, table td {
padding: 6px 13px;
border: 1px solid #ddd;
}
table th {
font-weight: bold;
}
table tr {
background-color: none;
border-top: 1px solid #ccc;
}
table tr:nth-child(2n) {
background-color: rgb(247, 247, 249);
}
.mce-item-table, .mce-item-table td, .mce-item-table th, .mce-item-table caption {
border: 1px solid #ddd;
border-collapse: collapse;
padding: 6px 13px;
}
blockquote {
border-left-width:10px;
background-color:rgba(128,128,128,0.05);
border-top-right-radius:5px;
border-bottom-right-radius:5px;
padding:15px 20px;
border-left:5px solid rgba(128,128,128,0.075);
}
blockquote p {
margin-bottom:1.1em;
font-size:1em;
line-height:1.45
}
blockquote ul:last-child,blockquote ol:last-child {
margin-bottom:0
}
pre {
padding: 18px;
background-color: #f7f7f9;
border: 1px solid #e1e1e8;
border-radius: 3px;
display: block;
}
code {
padding: 2px 4px;
font-size: 90%;
color: #c7254e;
white-space: nowrap;
background-color: #f9f2f4;
border-radius: 4px;
}
.footnote {
vertical-align: top;
position: relative;
top: -0.5em;
font-size: .8em;
}
hr {
margin:2em 0
}
img {
max-width:100%
}
pre {
word-break:break-word
}
p,pre,pre.prettyprint,blockquote {
margin:0 0 1.1em
}
hr {
margin:2em 0
}
img {
max-width:100%
}
.sequence-diagram,.flow-chart {
text-align:center;
margin-bottom:1.1em
}
.sequence-diagram text,.flow-chart text {
font-size:15px !important;
font-family:"Source Sans Pro",sans-serif !important
}
.sequence-diagram [fill="#ffffff"],.flow-chart [fill="#ffffff"] {
fill:#f6f6f6
}
.sequence-diagram [stroke="#000000"],.flow-chart [stroke="#000000"] {
stroke:#3f3f3f
}
.sequence-diagram text[stroke="#000000"],.flow-chart text[stroke="#000000"] {
stroke:none
}
.sequence-diagram [fill="#000"],.flow-chart [fill="#000"],.sequence-diagram [fill="#000000"],.flow-chart [fill="#000000"],.sequence-diagram [fill="black"],.flow-chart [fill="black"] {
fill:#3f3f3f
}
ul,ol {
margin-bottom:1.1em
}
ul ul,ol ul,ul ol,ol ol {
margin-bottom:1.1em
}
kbd {
padding:.1em .6em;
border:1px solid rgba(63,63,63,0.25);
-webkit-box-shadow:0 1px 0 rgba(63,63,63,0.25);
box-shadow:0 1px 0 rgba(63,63,63,0.25);
font-size:.7em;
font-family:sans-serif;
background-color:#fff;
color:#333;
border-radius:3px;
display:inline-block;
margin:0 .1em;
white-space:nowrap
}
.toc ul {
list-style-type:none;
margin-bottom:15px
}
</style>
<!-- 该css供自定义样式 -->
<link href="../leanote-html.css" rel="stylesheet">
</head>
<body>
<div class="note-container">
<h1 class="title" id="leanote-title">python路径常用函数</h1>
<div class="content-html" id="leanote-content"><div># -- coding:utf-8 --</div><div># python路径常用函数 os.path #</div><div><br></div><div>import os</div><div><br></div><div>#返回目录或者文件名 = os.path.split(path)[1]</div><div>os.path.basename(path)</div><div><br></div><div>#返回文件目录或者路径的父目录 = os.path.split(path)[0]</div><div>os.path.dirname(path)</div><div><br></div><div>#判断文件、目录是否存在</div><div>os.path.exists(path)</div><div><br></div><div>#拼接路径, 以系统分隔符 (os.sep)</div><div>os.path.join(path1,path2)</div><div><br></div><div>#规范化路径,将反斜杠转为斜杠,字母转为小写</div><div>os.path.normcase('c:\Test') #'c:/test'&nbsp;</div><div><br></div><div>#将路径分为一个元组以最后一个斜杠为分界线如果最后一个字符为斜杠那么返回的第2个元素为空</div><div>os.path.split('c:/1/') &nbsp;#('c:/1', '')</div><div>os.path.split('c:/1') &nbsp;#('c:/', '1')</div><div>os.path.split('c:/1/1.txt') #('c:/1', '1.txt')</div><div><br></div><div>#将路径转换为一个元祖,如果为目录则第二个元素为空,如果文件则第二个元素为文件扩展名</div><div>os.path.splitext('c:/1') #('c:/1, '')</div><div>os.path.splitext('c:/1/1.txt') #('c:/1', '.txt')</div><div><br></div><div>#os.path.walk(path, visit, arg)&nbsp;</div><div>#遍历目录及子目录</div><div>#path:待遍历根目录 visit(arg, dirname, names) 遍历目录的函数 dirname目录名names目录下文件名(也包含目录)字列表</div><div>def showfiles(arg, dirname, names):</div><div>print "目录:%s" % dirname</div><div>#os.path.join拼接路径</div><div>#os.path.isfile判断是否为文件 os.path.isdir判断是否为目录</div><div>names=[n for n in names if os.path.isfile(os.path.join(dirname,n))]</div><div>print "目录中文件:%s" % ','.join(names)</div><div>os.path.walk('E:/python/walk', showfiles, '')</div><div><br></div><div><br></div><div><br></div><div>一 常用函数</div><div>os模块</div><div>os.sep 表示默认的文件路径分隔符windows为\,&nbsp;<a href="http://lib.csdn.net/base/linux" data-mce-href="http://lib.csdn.net/base/linux">Linux</a>为/</div><div>os.walk(spath): 用来遍历目录下的文件和子目录</div><div>os.listdir(dirname)列出dirname下的目录和文件</div><div>os.mkdir() 创建目录</div><div>os.makedirs(): 创建目录,包含中间级目录</div><div>os.remove删除文件不能是目录</div><div>os.rmdir删除空目录</div><div>os.removedirs(path):删除目录及其子目录</div><div>os.rename(src, dst) :修改文件名</div><div>os.renames(old, new) :修改文件或目录名,包含中间级</div><div>os.chdir("/tmp") 更改当前目录</div><div>os.chmod( "c:\\test\\buildid.txt", stat.S_IWRITE ) 去除文件的只读属性</div><div><br></div><div>os.path模块</div><div>os.path.pathsep 表示默认的路径间的分隔符windows为; Linux为</div><div>os.path.isdir(name):判断name是不是一个目录name不是目录就返回false</div><div>os.path.isfile(name):判断name是不是一个文件不存在name也返回false</div><div>os.path.exists(name):判断是否存在文件或目录name</div><div>os.path.getsize(name):获得文件大小如果name是目录返回0L</div><div>os.path.getctime(name):获得文件的创建时间</div><div>os.path.getmtime(name):获得文件的修改时间</div><div>os.path.getatime(name):获得文件的最后访问时间</div><div>&nbsp;</div><div>os.path.isabs(name):<a href="http://lib.csdn.net/base/softwaretest" data-mce-href="http://lib.csdn.net/base/softwaretest">测试</a>是否是绝对路径</div><div>os.path.abspath(name):获得绝对路径</div><div>os.path.normpath(path):规范path字符串形式</div><div>os.path.relpath(path, start='.'):返回路径的相对版本</div><div><br></div><div>os.path.split(name):分割文件名与目录(事实上,如果你完全使用目录,它也会将最后一个目录作为文件名而分离,同时它不会判断文件或目录是否存在)</div><div>os.path.splitext():分离文件名与扩展名</div><div>os.path.splitdrive():分离驱动名或unc名字</div><div>os.path.join(path,name):连接目录与文件名或目录</div><div><br></div><div>os.path.basename(path):返回文件名</div><div>os.path.dirname(path):返回文件路径</div><div>os.path.expanduser("~")用来获得user的home路径。</div><div><br></div><div><br></div><div>shutil模块</div><div>shutil.copyfile(src, dst): 拷贝文件</div><div>shutil.copytree(srcDir, dstDir) : 拷贝目录</div><div>&nbsp;</div><div>shutil.rmtree('dir') : 删除非空文件夹</div><div>shutil.move('old','new') :修改文件和目录名称</div><div>&nbsp;</div><div>glob模块</div><div>匹配文件glob.glob(r”c:\linuxany\*.py”)</div></div>
</div>
<!-- 该js供其它处理 -->
<script src="../leanote-html.js"></script>
</body>
</html>