python:ospackage
This is an old revision of the document!
Table of Contents
os package for file and directory
os package
- Executing a shell command
os.system()
- Get the users environment
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
os.rename(src, dst)
- Remove (delete) the directory path
os.rmdir(path)
os.path
os.path basic functions
- split:
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)
output:
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')
- using os.path to check what a filename represents
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
output:
. => EXISTS ISDIR / => EXISTS ISABS ISDIR ISMOUNT file => /file => ISABS samples => EXISTS ISDIR samples/sample.jpg => EXISTS ISFILE directory/file => ../directory/file => /directory/file => ISABS
- using os.path.walk list all file in current directory and all subdirectories
import os def gotodir(arg, directory, files): for file in files: print file os.path.walk(".", gotodir, "")
Output
.project .pydevproject conent.tpl index.html parser.py test.py
os.path custom examples
- Using os.path to handle filename
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'
⇒ output
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
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)
python/ospackage.1406861572.txt.gz · Last modified: 2022/10/29 16:15 (external edit)