os.system()
os.environ()
os.getcwd()
os.listdir(path)
os.mkdir(path)
os.makedirs(path)
os.remove(path)
os.removedirs(path)
os.rename(src, dst)
os.rmdir(path)
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')
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
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 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
output:
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
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)
output:
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
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
import os def gotodir(arg, directory, files): for file in files: fullfilename = os.path.join(directory,file) print fullfilename os.path.walk(".", gotodir, "")
import os def gotodir(arg, directory, files): for file in files: extName = os.path.splitext(file)[1]; if extName == '.py': fullfilename = os.path.join(directory,file) print fullfilename os.path.walk(".", gotodir, "")
import os fullpathexecute = sys.argv[0] pathofexecute = os.path.split(fullpathexecute)[0] print pathofexecute
with open(filename, "rb") as f: content = f.read() if content != "": #content = content.replace(oldstring, newstring) with open(filename, "wb") as f: f.write(content)
refer: https://www.tutorialspoint.com/python/python_command_line_arguments.htm
Consider we want to pass two file names through command line and we also want to give an option to check the usage of the script. Usage of the script is as follows −
usage: test.py -i <inputfile> -o <outputfile>
Here is the following script to test.py −
#!/usr/bin/python import sys, getopt def main(argv): inputfile = '' outputfile = '' try: opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="]) except getopt.GetoptError: print 'test.py -i <inputfile> -o <outputfile>' sys.exit(2) for opt, arg in opts: if opt == '-h': print 'test.py -i <inputfile> -o <outputfile>' sys.exit() elif opt in ("-i", "--ifile"): inputfile = arg elif opt in ("-o", "--ofile"): outputfile = arg print 'Input file is "', inputfile print 'Output file is "', outputfile if __name__ == "__main__": main(sys.argv[1:])
Now, run above script as follows −
$ test.py -h
output:
test.py -i <inputfile> -o <outputfile>
$ test.py -i inputfile
output:
Input file is " inputfile Output file is "