python的os模块学习
Process Parameters
os.environ(环境变量)
1>>> os.environ['HOME'] 2'/home/xxx'
os.getenv(key, default=None)
1>>> os.getenv("HOME") 2'/home/xxx'
os.path
os.path.abspath(path) #返回绝对路径
1>>> os.path.abspath('aa.py') 2'/home/jianglin/test/aa.py'
os.path.basename(path) #返回文件名
1>>> os.path.basename('aa.py') 2'aa.py'
os.path.commonprefix(list) #返回list(多个路径)中,所有path共有的最长的路径。
1>>> os.path.commonprefix(['aa.py','a.py']) 2'a' 3>>> os.path.commonprefix(['aa.py','c.py']) 4'' 5>>> os.path.commonprefix(['~/test/aa.py','~/test/c.py']) 6'~/test/' 7>>> os.path.commonprefix(['~/test/aa.py','~/test/c.py','~/test/git/.git/config']) 8'~/test/'
os.path.dirname(path) #返回文件路径
1>>> os.path.dirname('~/test/aa.py') 2'~/test'
os.path.exists(path) #路径存在则返回True,路径不存在返回False
os.path.exists('aa.py')
1True 2>>> os.path.exists('aa.pp') 3False
os.path.lexists #路径存在则返回True,路径不存在也返回False
1>>> os.path.lexists('aa.py') 2True 3>>> os.path.lexists('aa.pp') 4False
os.path.expanduser(path) #把path中包含的"~"和"~user"转换成用户目录
1>>> os.path.expanduser('aa.py') 2'aa.py' 3>>> os.path.expanduser('~/test/aa.py') 4'/home/jianglin/test/aa.py'
os.path.expandvars(path) #根据环境变量的值替换path中包含的”(name”和”){name}”
os.path.getatime(path) #返回最后一次进入此path的时间。
1>>> os.path.getatime('aa.py') 21461933601.0809627 3>>> os.path.getatime('~/git/') 4Traceback (most recent call last): 5File "<stdin>", line 1, in <module> 6File "/usr/lib/python3.5/genericpath.py", line 60, in getatime 7return os.stat(filename).st_atime 8FileNotFoundError: [Errno 2] No such file or directory: '~/git/' 9>>> os.path.getatime('~/git/maple-bbs/.gitignore') 10Traceback (most recent call last): 11File "<stdin>", line 1, in <module> 12File "/usr/lib/python3.5/genericpath.py", line 60, in getatime 13return os.stat(filename).st_atime 14FileNotFoundError: [Errno 2] No such file or directory: '~/git/maple-bbs/.gitignore' 15>>> os.path.getatime('a.py') 161461418983.486829 17>>> os.path.getatime('~/git') 18Traceback (most recent call last): 19File "<stdin>", line 1, in <module> 20File "/usr/lib/python3.5/genericpath.py", line 60, in getatime 21return os.stat(filename).st_atime 22FileNotFoundError: [Errno 2] No such file or directory: '~/git' 23>>> os.path.getatime('./git/.git/config') 241461856107.9136002
os.path.getmtime(path) #返回在此path下最后一次修改的时间。
1>>> os.path.getmtime('aa.py') 21461933858.6676357
os.path.getctime(path)
1>>> os.path.getctime('aa.py') 21461933858.6676357
os.path.getsize(path) #返回文件大小,如果文件不存在就返回错误
1>>> os.path.getsize('aa.py') 2448
os.path.isabs(path) #判断是否为绝对路径
1>>> os.path.isabs('aa.py') 2False 3>>> os.path.isabs('~/test/aa.py') 4False 5>>> os.path.isabs('/home/jianglin/test/aa.py') 6True
os.path.isfile(path) #判断路径是否为文件
1>>> os.path.isfile('aa.py') 2True 3>>> os.path.isfile('~/test') 4False
os.path.isdir(path) #判断路径是否为目录
1>>> os.path.isdir('~/test') 2False 3>>> os.path.isdir('/home/jianglin/test/aa.py') 4False 5>>> os.path.isdir('/home/jianglin/test') 6True
os.path.islink(path) #判断路径是否为链接
os.path.ismount(path) #判断路径是否为挂载点()
os.path.join(path1[, path2[, …]]) #把目录和文件名合成一个路径
1>>> os.path.join('/home/jianglin/test','aa.py') 2'/home/jianglin/test/aa.py' 3>>> os.path.join('/home/jianglin','test/aa.py') 4'/home/jianglin/test/aa.py' 5>>> os.path.join('~','test/aa.py') 6'~/test/aa.py'
os.path.normcase(path) #转换path的大小写和斜杠
os.path.normpath(path) #规范path字符串形式
os.path.realpath(path) #返回path的真实路径
os.path.relpath(path[, start]) #从start开始计算相对路径
os.path.samefile(path1, path2) #判断目录或文件是否相同
os.path.sameopenfile(fp1, fp2) #判断fp1和fp2是否指向同一文件
os.path.samestat(stat1, stat2) #判断stat tuple stat1和stat2是否指向同一个文件
os.path.split(path) #把路径分割成dirname和basename,返回一个元组
1>>> os.path.split('~/test/aa.py') 2('~/test', 'aa.py')
os.path.splitdrive(path) #一般用在windows下,返回驱动器名和路径组成的元组
os.path.splitext(path) #分割路径,返回路径名和文件扩展名的元组
os.path.splitunc(path) #把路径分割为加载点与文件
os.path.walk(path, visit, arg) #遍历path,进入每个目录都调用visit函数,visit函数必须有
3个参数(arg, dirname, names),dirname表示当前目录的目录名,names代表当前目录下的所有
文件名,args则为walk的第三个参数
