操作系统模块扩展,仅适用于POSIX系统
OSExtension的Python项目详细描述
filesystem模块
importfilesystemasfs
测试路径是否为文件(快捷方式为try/with open):
fs.isfile('something')fs.isfile('something',mode='r')
rsync -a
的快捷方式使用`sh模块<;https://pypi.python.org/pypi/sh/>;`
fs.sync('name@somehost:~/somedir/','local_path')fs.sync('~/somedir','.')
返回退出代码,不捕获由^ {TT6}$引发的任何异常。
根据修改时间检查文件是否相同
示例使用:基于HEADHTTP确定文件是否相同 使用Last-Modified头请求
fromurllib2.requestimporturlopenreq=urlopen('http://i.imgur.com/sgon5YP.jpg')req.get_method=lambda:'HEAD'last_modified=Noneforlineinstr(req.info()).split('\n'):if'last-modified'inline.lower():last_modified=line.split(': ')[1].strip()last_modified=time.strptime(last_modified.replace(' GMT',''),'%a, %d %b %Y %H:%M:%S')break# Actual checkfs.has_same_time('./sgon5YP.jpg',last_modified)
删除一组文件
使用fs.rm_files(list_of_files, raise_on_error=bool_val)。
pushd与with语句一起使用
fromosext.pushdcontextimportpushdwithpushd('some_dir')ascontext:pass