User Tools

Site Tools


python:ospackage

Differences

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

Link to this comparison view

Next revision
Previous revision
python:ospackage [2014/07/07 12:44] – created 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 ===== 
-  * 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> 
-  * 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> 
-  * 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> 
python/ospackage.1404737059.txt.gz · Last modified: 2022/10/29 16:15 (external edit)