python:ospackage
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
python:ospackage [2015/10/08 09:14] – [os.path custom examples] admin | python:ospackage [2015/10/24 02:21] (current) – removed admin | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== os package for file and directory ====== | ||
- | ===== os package ===== | ||
- | * Executing a shell command< | ||
- | os.system()</ | ||
- | * Get the users environment <code python> | ||
- | os.environ()</ | ||
- | * Returns the current working directory< | ||
- | os.getcwd()</ | ||
- | * Return a list of the entries in the directory given by path< | ||
- | os.listdir(path)</ | ||
- | * Create a directory named path with numeric mode mode< | ||
- | os.mkdir(path)</ | ||
- | * Recursive directory creation function< | ||
- | os.makedirs(path)</ | ||
- | * Remove (delete) the file path< | ||
- | os.remove(path)</ | ||
- | * Remove directories recursively< | ||
- | os.removedirs(path)</ | ||
- | * Rename the file or directory src to dst<code python> | ||
- | os.rename(src, | ||
- | * Remove (delete) the directory path< | ||
- | os.rmdir(path)</ | ||
- | ===== os.path ===== | ||
- | ==== os.path basic functions ==== | ||
- | * split:< | ||
- | import os | ||
- | from urlparse import urlparse | ||
- | |||
- | filename = " | ||
- | print "full filename", | ||
- | print " | ||
- | print " | ||
- | print " | ||
- | print " | ||
- | print " | ||
- | </ | ||
- | full filename E: | ||
- | using nt ... | ||
- | split => (' | ||
- | splitdrive => (' | ||
- | splitext => (' | ||
- | splitunc => ('', | ||
- | </ | ||
- | * using os.path to check what a filename represents< | ||
- | import os | ||
- | |||
- | FILES = ( | ||
- | os.curdir, | ||
- | "/", | ||
- | " | ||
- | "/ | ||
- | " | ||
- | " | ||
- | " | ||
- | " | ||
- | "/ | ||
- | ) | ||
- | |||
- | for file in FILES: | ||
- | print file, " | ||
- | if os.path.exists(file): | ||
- | print " | ||
- | if os.path.isabs(file): | ||
- | print " | ||
- | if os.path.isdir(file): | ||
- | print " | ||
- | if os.path.isfile(file): | ||
- | print " | ||
- | if os.path.islink(file): | ||
- | print " | ||
- | if os.path.ismount(file): | ||
- | print " | ||
- | |||
- | </ | ||
- | . => EXISTS ISDIR | ||
- | / => EXISTS ISABS ISDIR ISMOUNT | ||
- | file => | ||
- | /file => ISABS | ||
- | samples => EXISTS ISDIR | ||
- | samples/ | ||
- | directory/ | ||
- | ../ | ||
- | / | ||
- | </ | ||
- | |||
- | ==== os.path custom examples ==== | ||
- | * Using os.path to handle filename< | ||
- | import os | ||
- | import re | ||
- | |||
- | filename = " | ||
- | |||
- | print " | ||
- | print " | ||
- | print " | ||
- | print " | ||
- | print " | ||
- | print " | ||
- | | ||
- | print " | ||
- | print " | ||
- | print " | ||
- | using nt ... | ||
- | split => (' | ||
- | splitext => (' | ||
- | dirname => my/little | ||
- | basename => pony.txt | ||
- | join => my/ | ||
- | realpath E: | ||
- | abspath E: | ||
- | rename filename to my/ | ||
- | </ | ||
- | * get normal path< | ||
- | import os | ||
- | |||
- | filename = " | ||
- | |||
- | print "full filename", | ||
- | print " | ||
- | print " | ||
- | print " | ||
- | fullpathname = os.path.dirname(filename) | ||
- | pathname = os.path.splitdrive(fullpathname)[1] | ||
- | print " | ||
- | normalpath = pathname[1: | ||
- | print " | ||
- | </ | ||
- | full filename E: | ||
- | basename the-hinh-chu-so-vua-hoc-vua-choi.html | ||
- | dirname E: | ||
- | splitdrive => (' | ||
- | pathname = \do-choi\do-choi-giay | ||
- | normalpath = do-choi\do-choi-giay | ||
- | </ | ||
- | * urlparse< | ||
- | import os | ||
- | from urlparse import urlparse | ||
- | |||
- | url = " | ||
- | o = urlparse(url) | ||
- | print o | ||
- | filename = o.path | ||
- | print filename | ||
- | print " | ||
- | print " | ||
- | print " | ||
- | print " | ||
- | print " | ||
- | | ||
- | print " | ||
- | print " | ||
- | </ | ||
- | ParseResult(scheme=' | ||
- | / | ||
- | split => ('/ | ||
- | splitext => ('/ | ||
- | dirname => / | ||
- | basename => the-hinh-chu-so-vua-hoc-vua-choi.html | ||
- | join => / | ||
- | realpath E: | ||
- | abspath E: | ||
- | </ | ||
- | ==== Get all files and directories in current directory ==== | ||
- | * using os.path.walk list all file in current directory and all subdirectories< | ||
- | import os | ||
- | |||
- | def gotodir(arg, | ||
- | for file in files: | ||
- | print file | ||
- | |||
- | os.path.walk(" | ||
- | </ | ||
- | .project | ||
- | .pydevproject | ||
- | conent.tpl | ||
- | index.html | ||
- | parser.py | ||
- | test.py | ||
- | </ | ||
- | * using os.path.walk list all full filename in current directory and all subdirectories< | ||
- | import os | ||
- | |||
- | def gotodir(arg, | ||
- | for file in files: | ||
- | fullfilename = os.path.join(directory, | ||
- | print fullfilename | ||
- | |||
- | os.path.walk(" | ||
- | </ | ||
- | * Find all files with extension " | ||
- | import os | ||
- | |||
- | def gotodir(arg, | ||
- | for file in files: | ||
- | extName = os.path.splitext(file)[1]; | ||
- | if extName == ' | ||
- | fullfilename = os.path.join(directory, | ||
- | print fullfilename | ||
- | |||
- | os.path.walk(" | ||
- | </ |
python/ospackage.1444295647.txt.gz · Last modified: 2022/10/29 16:15 (external edit)