1. Get the current working directory in Python
CurDir = OS.GETCWD ()
OS.GETCWD () returns the directory where the command was executed, not the directory in which the script itself resides
2, Os.path
- Os.path.abspath (path) #返回绝对路径
- Os.path.split (path) #将path分割成目录和文件名二元组返回
- Os.path.dirname (path) #返回path的目录. is actually the first element of Os.path.split (path)
- Os.path.basename (path) #返回path最后的文件名
- Os.path.exists (path) #如果path存在, returns True if path does not exist, returns false
- Os.path.isabs (path) #如果path是绝对路径, returns True
- Os.path.isfile (path) #如果path是一个存在的文件, returns True. Otherwise returns false
- Os.path.isdir (path) #如果path是一个存在的目录, returns True. Otherwise returns false
- Os.path.getatime (path) #返回path所指向的文件或者目录的最后存取时间
- Os.path.getmtime (path) #返回path所指向的文件或者目录的最后修改时间
- Os.path.curdir #返回当前目录 (relative path)
- Os.path.pardir #返回父目录
- S.path.join (path1[, path2[, ...]) #将多个路径组合后返回, the parameters before the first absolute path are ignored.
- >>> os.path.join (' c:\\ ', ' csv ', ' test.csv ')
- ' C:\\csv\\test.csv '
- >>> os.path.join (' windows\temp ', ' c:\\ ', ' csv', ' test.csv ')
- ' C:\\csv\\test.csv '
- >>> os.path.join ('/home/aa ','/home/aa/bb ','/home/aa/bb/c ')
- '/home/aa/bb/c '
Introduction to Os.path modules in Python