User Tools

Site Tools


python:ospackage

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
python:ospackage [2015/10/08 09:05] – [os.path custom examples] adminpython: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<code python> 
-os.system()</code> 
-  * Get the users environment <code python> 
-os.environ()</code> 
-  * Returns the current working directory<code python> 
-os.getcwd()</code> 
-  * Return a list of the entries in the directory given by path<code python> 
-os.listdir(path)</code> 
-  * Create a directory named path with numeric mode mode<code python> 
-os.mkdir(path)</code> 
-  * Recursive directory creation function<code python> 
-os.makedirs(path)</code> 
-  * Remove (delete) the file path<code python> 
-os.remove(path)</code> 
-  * Remove directories recursively<code python> 
-os.removedirs(path)</code> 
-  * Rename the file or directory src to dst<code python> 
-os.rename(src, dst)</code> 
-  * Remove (delete) the directory path<code python> 
-os.rmdir(path)</code>  
-===== os.path ===== 
-==== os.path basic functions ==== 
-  * split:<code python> 
-import os 
-from urlparse import urlparse 
-  
-filename = "E:\\do-choi\\do-choi-giay\\the-hinh-chu-so-vua-hoc-vua-choi.html" 
  
-print "full filename", filename 
-print "using", os.name, "..." 
-print "split", "=>", os.path.split(filename) 
-print "splitdrive", "=>", os.path.splitdrive(filename) 
-print "splitext", "=>",os.path.splitext(filename) 
-print "splitunc", "=>",os.path.splitunc(filename) 
-</code>output:<code> 
-full filename E:\do-choi\do-choi-giay\the-hinh-chu-so-vua-hoc-vua-choi.html 
-using nt ... 
-split => ('E:\\do-choi\\do-choi-giay', 'the-hinh-chu-so-vua-hoc-vua-choi.html') 
-splitdrive => ('E:', '\\do-choi\\do-choi-giay\\the-hinh-chu-so-vua-hoc-vua-choi.html') 
-splitext => ('E:\\do-choi\\do-choi-giay\\the-hinh-chu-so-vua-hoc-vua-choi', '.html') 
-splitunc => ('', 'E:\\do-choi\\do-choi-giay\\the-hinh-chu-so-vua-hoc-vua-choi.html') 
-</code> 
-  * using os.path to check what a filename represents<code python> 
-import os 
- 
-FILES = ( 
-    os.curdir, 
-    "/", 
-    "file", 
-    "/file", 
-    "samples", 
-    "samples/sample.jpg", 
-    "directory/file", 
-    "../directory/file", 
-    "/directory/file" 
-    ) 
- 
-for file in FILES: 
-    print file, "=>", 
-    if os.path.exists(file): 
-        print "EXISTS", 
-    if os.path.isabs(file): 
-        print "ISABS", 
-    if os.path.isdir(file): 
-        print "ISDIR", 
-    if os.path.isfile(file): 
-        print "ISFILE", 
-    if os.path.islink(file): 
-        print "ISLINK", 
-    if os.path.ismount(file): 
-        print "ISMOUNT", 
-    print 
-</code>output:<code> 
-. => EXISTS ISDIR 
-/ => EXISTS ISABS ISDIR ISMOUNT 
-file => 
-/file => ISABS 
-samples => EXISTS ISDIR 
-samples/sample.jpg => EXISTS ISFILE 
-directory/file => 
-../directory/file => 
-/directory/file => ISABS 
-</code> 
- 
-==== os.path custom examples ==== 
-  * Using os.path to handle filename<code python> 
-import os 
-import re 
-  
-filename = "my/little/pony.txt" 
-  
-print "using", os.name, "..." 
-print "split", "=>", os.path.split(filename) 
-print "splitext", "=>", os.path.splitext(filename) 
-print "dirname", "=>", os.path.dirname(filename) 
-print "basename", "=>", os.path.basename(filename) 
-print "join", "=>", os.path.join(os.path.dirname(filename), 
-                                 os.path.basename(filename)) 
-print "realpath", os.path.realpath(filename) 
-print "abspath", os.path.abspath(filename) 
-print "rename filename to ", re.sub(r'\.txt', '', filename) + '.html'</code> => output<code> 
-using nt ... 
-split => ('my/little', 'pony.txt') 
-splitext => ('my/little/pony', '.txt') 
-dirname => my/little 
-basename => pony.txt 
-join => my/little\pony.txt 
-realpath E:\backup\GoogleDrive\projects\python\my\little\pony.txt 
-abspath E:\backup\GoogleDrive\projects\python\my\little\pony.txt 
-rename filename to  my/little/pony.html 
-</code> 
-  * get normal path<code python> 
-import os 
-  
-filename = "E:\\do-choi\\do-choi-giay\\the-hinh-chu-so-vua-hoc-vua-choi.html" 
- 
-print "full filename", filename 
-print "basename", os.path.basename(filename) 
-print "dirname", os.path.dirname(filename) 
-print "splitdrive", "=>", os.path.splitdrive(filename) 
-fullpathname = os.path.dirname(filename) 
-pathname = os.path.splitdrive(fullpathname)[1] 
-print "pathname = ", pathname 
-normalpath = pathname[1:] 
-print "normalpath = ", normalpath 
-</code>output:<code> 
-full filename E:\do-choi\do-choi-giay\the-hinh-chu-so-vua-hoc-vua-choi.html 
-basename the-hinh-chu-so-vua-hoc-vua-choi.html 
-dirname E:\do-choi\do-choi-giay 
-splitdrive => ('E:', '\\do-choi\\do-choi-giay\\the-hinh-chu-so-vua-hoc-vua-choi.html') 
-pathname =  \do-choi\do-choi-giay 
-normalpath =  do-choi\do-choi-giay 
-</code> 
-  * urlparse<code python> 
-import os 
-from urlparse import urlparse 
-  
-url = "http://shop.babies.vn/do-choi/do-choi-giay/the-hinh-chu-so-vua-hoc-vua-choi.html" 
-o = urlparse(url) 
-print o 
-filename = o.path 
-print filename 
-print "split", "=>", os.path.split(filename) 
-print "splitext", "=>", os.path.splitext(filename) 
-print "dirname", "=>", os.path.dirname(filename) 
-print "basename", "=>", os.path.basename(filename) 
-print "join", "=>", os.path.join(os.path.dirname(filename), 
-                                 os.path.basename(filename)) 
-print "realpath", os.path.realpath(filename) 
-print "abspath", os.path.abspath(filename) 
-</code>output:<code> 
-ParseResult(scheme='http', netloc='shop.babies.vn', path='/do-choi/do-choi-giay/the-hinh-chu-so-vua-hoc-vua-choi.html', params='', query='', fragment='') 
-/do-choi/do-choi-giay/the-hinh-chu-so-vua-hoc-vua-choi.html 
-split => ('/do-choi/do-choi-giay', 'the-hinh-chu-so-vua-hoc-vua-choi.html') 
-splitext => ('/do-choi/do-choi-giay/the-hinh-chu-so-vua-hoc-vua-choi', '.html') 
-dirname => /do-choi/do-choi-giay 
-basename => the-hinh-chu-so-vua-hoc-vua-choi.html 
-join => /do-choi/do-choi-giay\the-hinh-chu-so-vua-hoc-vua-choi.html 
-realpath E:\do-choi\do-choi-giay\the-hinh-chu-so-vua-hoc-vua-choi.html 
-abspath E:\do-choi\do-choi-giay\the-hinh-chu-so-vua-hoc-vua-choi.html 
-</code> 
-  * using os.path.walk list all file in current directory and all subdirectories<code python> 
-import os 
- 
-def gotodir(arg, directory, files): 
-    for file in files: 
-        print file 
- 
-os.path.walk(".", gotodir, "") 
-</code>Output<code> 
-.project 
-.pydevproject 
-conent.tpl 
-index.html 
-parser.py 
-test.py 
-</code> 
-  * using os.path.walk list all full filename in current directory and all subdirectories<code python> 
-import os 
-  
-def gotodir(arg, directory, files): 
-    for file in files: 
-        fullfilename = os.path.join(directory,file) 
-        print fullfilename 
-  
-os.path.walk(".", gotodir, "") 
-</code> 
python/ospackage.1444295144.txt.gz · Last modified: 2022/10/29 16:15 (external edit)