{"id":661,"date":"2014-07-20T10:43:37","date_gmt":"2014-07-20T10:43:37","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=661"},"modified":"2014-07-20T10:43:37","modified_gmt":"2014-07-20T10:43:37","slug":"python%e7%9a%84os-path%e6%a8%a1%e5%9d%97","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/661.html","title":{"rendered":"Python\u7684os.path\u6a21\u5757"},"content":{"rendered":"<h4>Python\u7684os.path\u6a21\u5757<\/h4>\n<ul>\n<li>os.path.abspath(path) #\u8fd4\u56de\u7edd\u5bf9\u8def\u5f84<\/li>\n<li>os.path.basename(path) #\u8fd4\u56de\u6587\u4ef6\u540d<\/li>\n<li>os.path.commonprefix(list) #\u8fd4\u56delist(\u591a\u4e2a\u8def\u5f84)\u4e2d\uff0c\u6240\u6709path\u5171\u6709\u7684\u6700\u957f\u7684\u8def\u5f84\u3002<\/li>\n<li>os.path.<span style=\"color: #ff0000;\">dirname<\/span>(path) #\u8fd4\u56de\u6587\u4ef6\u8def\u5f84<\/li>\n<li>os.path.<span style=\"color: #ff0000;\"><strong>exists<\/strong><\/span>(path) #\u8def\u5f84\u5b58\u5728\u5219\u8fd4\u56deTrue,\u8def\u5f84\u635f\u574f\u8fd4\u56deFalse<\/li>\n<li>os.path.<span style=\"color: #ff0000;\"><strong>lexists<\/strong><\/span> #\u8def\u5f84\u5b58\u5728\u5219\u8fd4\u56deTrue,\u8def\u5f84\u635f\u574f\u4e5f\u8fd4\u56deTrue<\/li>\n<li>os.path.expanduser(path) #\u628apath\u4e2d\u5305\u542b\u7684&#8221;~&#8221;\u548c&#8221;~user&#8221;\u8f6c\u6362\u6210\u7528\u6237\u76ee\u5f55<\/li>\n<li>os.path.expandvars(path) #\u6839\u636e\u73af\u5883\u53d8\u91cf\u7684\u503c\u66ff\u6362path\u4e2d\u5305\u542b\u7684\u201d$name\u201d\u548c\u201d${name}\u201d<\/li>\n<li>os.path.getatime(path) #\u8fd4\u56de\u6700\u540e\u4e00\u6b21\u8fdb\u5165\u6b64path\u7684\u65f6\u95f4\u3002<\/li>\n<li>os.path.getmtime(path) #\u8fd4\u56de\u5728\u6b64path\u4e0b\u6700\u540e\u4e00\u6b21\u4fee\u6539\u7684\u65f6\u95f4\u3002<\/li>\n<li>os.path.getctime(path) #\u8fd4\u56depath\u7684\u5927\u5c0f<\/li>\n<li>os.path.getsize(path) #\u8fd4\u56de\u6587\u4ef6\u5927\u5c0f\uff0c\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\u5c31\u8fd4\u56de\u9519\u8bef<\/li>\n<li>os.path.isabs(path) #\u5224\u65ad\u662f\u5426\u4e3a\u7edd\u5bf9\u8def\u5f84<\/li>\n<li>os.path.isfile(path) #\u5224\u65ad\u8def\u5f84\u662f\u5426\u4e3a\u6587\u4ef6<\/li>\n<li>os.path.isdir(path) #\u5224\u65ad\u8def\u5f84\u662f\u5426\u4e3a\u76ee\u5f55<\/li>\n<li>os.path.islink(path) #\u5224\u65ad\u8def\u5f84\u662f\u5426\u4e3a\u94fe\u63a5<\/li>\n<li>os.path.ismount(path) #\u5224\u65ad\u8def\u5f84\u662f\u5426\u4e3a\u6302\u8f7d\u70b9\uff08\uff09<\/li>\n<li>os.path.<strong><span style=\"color: #ff0000;\">join<\/span><\/strong>(path1[, path2[, &#8230;]]) #\u628a\u76ee\u5f55\u548c\u6587\u4ef6\u540d\u5408\u6210\u4e00\u4e2a\u8def\u5f84<\/li>\n<li>os.path.<span style=\"color: #ff0000;\">normcase<\/span>(path) #\u8f6c\u6362path\u7684\u5927\u5c0f\u5199\u548c\u659c\u6760<\/li>\n<li>os.path.<span style=\"color: #ff0000;\">normpath<\/span>(path) #\u89c4\u8303path\u5b57\u7b26\u4e32\u5f62\u5f0f<\/li>\n<li>os.path.realpath(path) #\u8fd4\u56depath\u7684\u771f\u5b9e\u8def\u5f84<\/li>\n<li>os.path.relpath(path[, start]) #\u4ecestart\u5f00\u59cb\u8ba1\u7b97\u76f8\u5bf9\u8def\u5f84<\/li>\n<li>os.path.samefile(path1, path2) #\u5224\u65ad\u76ee\u5f55\u6216\u6587\u4ef6\u662f\u5426\u76f8\u540c<\/li>\n<li>os.path.sameopenfile(fp1, fp2) #\u5224\u65adfp1\u548cfp2\u662f\u5426\u6307\u5411\u540c\u4e00\u6587\u4ef6<\/li>\n<li>os.path.samestat(stat1, stat2) #\u5224\u65adstat tuple stat1\u548cstat2\u662f\u5426\u6307\u5411\u540c\u4e00\u4e2a\u6587\u4ef6<\/li>\n<li>os.path.<span style=\"color: #ff0000;\"><strong>split<\/strong><\/span>(path) #\u628a\u8def\u5f84\u5206\u5272\u6210dirname\u548cbasename\uff0c\u8fd4\u56de\u4e00\u4e2a\u5143\u7ec4<\/li>\n<li>os.path.splitdrive(path) #\u4e00\u822c\u7528\u5728windows\u4e0b\uff0c\u8fd4\u56de\u9a71\u52a8\u5668\u540d\u548c\u8def\u5f84\u7ec4\u6210\u7684\u5143\u7ec4<\/li>\n<li>os.path.<span style=\"color: #ff0000;\"><strong>splitext<\/strong><\/span>(path) #\u5206\u5272\u8def\u5f84\uff0c\u8fd4\u56de\u8def\u5f84\u540d\u548c\u6587\u4ef6\u6269\u5c55\u540d\u7684\u5143\u7ec4<\/li>\n<li>os.path.splitunc(path) #\u628a\u8def\u5f84\u5206\u5272\u4e3a\u52a0\u8f7d\u70b9\u4e0e\u6587\u4ef6<\/li>\n<li>os.path.<span style=\"color: #ff0000;\"><strong>walk<\/strong><\/span>(path, visit, arg) #\u904d\u5386path\uff0c\u8fdb\u5165\u6bcf\u4e2a\u76ee\u5f55\u90fd\u8c03\u7528visit\u51fd\u6570\uff0cvisit\u51fd\u6570\u5fc5\u987b\u67093\u4e2a\u53c2\u6570(arg,dirname,names)\uff0cdirname\u8868\u793a\u5f53\u524d\u76ee\u5f55\u7684\u76ee\u5f55\u540d\uff0cnames\u4ee3\u8868\u5f53\u524d\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u540d\uff0cargs\u5219\u4e3awalk\u7684\u7b2c\u4e09\u4e2a\u53c2\u6570<\/li>\n<li>os.path.supports_unicode_filenames #\u8bbe\u7f6e\u662f\u5426\u652f\u6301unicode\u8def\u5f84\u540d<\/li>\n<\/ul>\n<pre class=\"lang:default decode:true\">'''\nC:Python27&gt;python\nPython 2.7.5 (default, May 15 2013, 22:44:16) [MSC v.1500 64 bit (AMD64)] on win32\nType \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n&gt;&gt;&gt; import os.path\n&gt;&gt;&gt; dir(os.path)\n['__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_abspath_split', '_getfullpathname', 'abspath', 'altsep', 'basename', 'commonprefix', 'curdir', 'defpath', 'devnull', 'dirname', 'exists', 'expanduser', 'expandvars', 'extsep', 'genericpath', 'getatime', 'getctime', 'getmtime', 'getsize', 'isabs', 'isdir', 'isfile', 'islink', 'ismount', 'join', 'lexists', 'normcase', 'normpath', 'os', 'pardir', 'pathsep', 'realpath', 'relpath', 'sep', 'split', 'splitdrive', 'splitext', 'splitunc', 'stat', 'supports_unicode_filenames', 'sys', 'walk', 'warnings']\n&gt;&gt;&gt;\n&gt;&gt;&gt; help(os.path)\nHelp on module ntpath:\n\nNAME\nntpath - Common pathname manipulations, WindowsNT\/95 version.\n\nFILE\nc:python27libntpath.py\n\nDESCRIPTION\nInstead of importing this module directly, import os and refer to this\nmodule as os.path.\n\nFUNCTIONS\nabspath(path)\nReturn the absolute version of a path.\n\nbasename(p)\nReturns the final component of a pathname\n\ncommonprefix(m)\nGiven a list of pathnames, returns the longest common leading component\n\ndirname(p)\nReturns the directory component of a pathname\n\nexists(path)\nTest whether a path exists. Returns False for broken symbolic links\n\nexpanduser(path)\nExpand ~ and ~user constructs.\n\nIf user or $HOME is unknown, do nothing.\n\nexpandvars(path)\nExpand shell variables of the forms $var, ${var} and %var%.\n\nUnknown variables are left unchanged.\n\ngetatime(filename)\nReturn the last access time of a file, reported by os.stat().\n\ngetctime(filename)\nReturn the metadata change time of a file, reported by os.stat().\n\ngetmtime(filename)\nReturn the last modification time of a file, reported by os.stat().\n\ngetsize(filename)\nReturn the size of a file, reported by os.stat().\n\nisabs(s)\nTest whether a path is absolute\n\nisdir = _isdir(...)\nReturn true if the pathname refers to an existing directory.\n\nisfile(path)\nTest whether a path is a regular file\n\nislink(path)\nTest for symbolic link.\nOn WindowsNT\/95 and OS\/2 always returns false\n\nismount(path)\nTest whether a path is a mount point (defined as root of drive)\n\njoin(a, *p)\nJoin two or more pathname components, inserting \"\" as needed.\nIf any component is an absolute path, all previous path components\nwill be discarded.\n\nlexists = exists(path)\nTest whether a path exists. Returns False for broken symbolic links\n\nnormcase(s)\nNormalize case of pathname.\n\nMakes all characters lowercase and all slashes into backslashes.\n\nnormpath(path)\nNormalize path, eliminating double slashes, etc.\n\nrealpath = abspath(path)\nReturn the absolute version of a path.\n\nrelpath(path, start='.')\nReturn a relative version of a path\n\nsplit(p)\nSplit a pathname.\n\nReturn tuple (head, tail) where tail is everything after the final slash.\nEither part may be empty.\n\nsplitdrive(p)\nSplit a pathname into drive and path specifiers. Returns a 2-tuple\n\"(drive,path)\"; either part may be empty\n\nsplitext(p)\nSplit the extension from a pathname.\n\nExtension is everything from the last dot to the end, ignoring\nleading dots. Returns \"(root, ext)\"; ext may be empty.\n\nsplitunc(p)\nSplit a pathname into UNC mount point and relative path specifiers.\n\nReturn a 2-tuple (unc, rest); either part may be empty.\nIf unc is not empty, it has the form '\/\/host\/mount' (or similar\nusing backslashes). unc+rest is always the input path.\nPaths containing drive letters never have an UNC part.\n\nwalk(top, func, arg)\nDirectory tree walk with callback function.\n\nFor each directory in the directory tree rooted at top (including top\nitself, but excluding '.' and '..'), call func(arg, dirname, fnames).\ndirname is the name of the directory, and fnames a list of the names of\nthe files and subdirectories in dirname (excluding '.' and '..'). func\nmay modify the fnames list in-place (e.g. via del or slice assignment),\nand walk will only recurse into the subdirectories whose names remain in\nfnames; this can be used to implement a filter, or to impose a specific\norder of visiting. No semantics are defined for, or required of, arg,\nbeyond that arg is always passed to func. It can be used, e.g., to pass\na filename pattern, or a mutable object designed to accumulate\nstatistics. Passing None for arg is common.\n\nDATA\n__all__ = ['normcase', 'isabs', 'join', 'splitdrive', 'split', 'splite...\naltsep = '\/'\ncurdir = '.'\ndefpath = r'.;C:bin'\ndevnull = 'nul'\nextsep = '.'\npardir = '..'\npathsep = ';'\nsep = r''\nsupports_unicode_filenames = True\n\n&gt;&gt;&gt;\n'''<\/pre>\n<h6>\u6279\u91cf\u91cd\u547d\u540d\uff1a<\/h6>\n<pre class=\"lang:default decode:true \">import os, time, glob\n\nfor files in glob.glob('D:\\photos\\*.jpg'):\n\tfilepath,filename = os.path.split(files)\n\tfilterame,exts = os.path.splitext(filename)\n\topfile = 'D:\\photos2\\'\t# output path\n\tif (os.path.isdir(opfile)==False):\n\t\tos.mkdir(opfile)\n\npath = os.path.normcase(\"D:\/photos2\/\")\ncc = time.gmtime()\nbmpname = str(cc[0])+str(cc[1])+str(cc[2])+str(cc[3]+8)+str(cc[4])+str(cc[5])+'.bmp'\nos.remove(bmpname)\t# remove .bmp file\njpgname = bmpname[:-4]+'.jpg'\ndjpgname = path+jpgname\ncopy_command = \"move %s %s\" % (jpgname, djpgname)\nos.popen(copy_command)<\/pre>\n<h6>os.walk\u51fd\u6570\u7684\u4e00\u6bb5\u4ee3\u7801\u793a\u4f8b\uff08\u7edf\u8ba1\u6307\u5b9a\u6587\u4ef6\u5939\u4e2d\u7684\u6587\u4ef6\u5927\u5c0f\uff09\uff1a<\/h6>\n<pre class=\"lang:default decode:true \">import os\nfrom os.path import join, getsize\nfor root, dirs, files in os.walk('python\/Lib\/email'):\n    print root, \"consumes\",\n    print sum([getsize(join(root, name)) for name in files]),\n    print \"bytes in\", len(files), \"non-directory files\"\n    if 'CVS' in dirs:\n        dirs.remove('CVS')  # don't visit CVS directories<\/pre>\n<p>&nbsp;<\/p>\n<h6>\u53c2\u8003\u7f51\u5740\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/blog.csdn.net\/hanshanyeyu\/article\/details\/7205203\" target=\"_blank\">os.path\u7528\u6cd5<\/a><\/li>\n<li><a href=\"http:\/\/www.cnblogs.com\/dkblog\/archive\/2011\/03\/25\/1995537.html\" target=\"_blank\">python os.path\u6a21\u5757<\/a><\/li>\n<li><a href=\"http:\/\/hongqn.livejournal.com\/13175.html\" target=\"_blank\">Python\u9677\u9631\uff1araw string<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Python\u7684os.path\u6a21\u5757 os.path.abspath(path) #\u8fd4\u56de\u7edd\u5bf9\u8def\u5f84 os.path. [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[252,8],"class_list":["post-661","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","tag-os-path","tag-python"],"views":2213,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/661","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=661"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/661\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=661"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=661"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=661"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}