python基础操做以及hdfs操做

1、前言

       做为一个全栈工程师,必需要熟练掌握各类语言。。。HelloWorld。最近就被“逼着”走向了python开发之路,大致实现的功能是写一个通用类库将服务器本地存储的文件进行简单清洗后转储到HDFS中,因此基本上python的相关知识都涉及到了,这里对一些基础操做以及hdfs操做作一总结,以备查阅。node

2、基础操做

2.1 字符串操做

       字符串操做应该是全部语言的基础。python基本上也提供了其余语言经常使用的一些字符串处理函数,经常使用的以下:python

一、startswith 以某个字符串起始服务器

二、endswith 以某个字符串结尾app

三、contain python没有提供contain函数,可使用 'test' in somestring 的方式来进行判断,固然也可使用index来判断函数

四、strip 去除空格及特殊符号spa

五、len 判断字符串长度len(str).net

六、upper lower 大小写转换code

七、split 分隔字符串blog

2.2 文件操做

       文件以及文件夹操做也是写程序中常常用到的功能。python中文件操做经常使用的有如下函数。递归

一、walk 用于递归遍历文件夹,获取全部文件。

二、os.path 文件、文件夹路径等操做。

       对文件操做进行了简单的封装,代码以下,仅供参考:

def isFile(name): return os.path.isfile(name) def isDir(name): return os.path.isdir(name) def getDirPath(filename): return os.path.dirname(filename) def getFilename(path): return os.path.basename(path) def getExt(filename): return os.path.splitext(filename)[1] def changeExt(filename, ext): if not ext.startswith('.'): ext = '.' + ext return getFilenameWithoutExt(filename) + ext def getDirAndFileNameWithoutExt(filename): return os.path.splitext(filename)[0] def getFilenameWithoutExt(filename): return getFilename(getDirAndFileNameWithoutExt(filename)) def deleteFileOrFolder(path): try: if isFile(path): os.remove(path) elif isDir(path): shutil.rmtree(path) # or os.rmdir(path) except: pass

2.3 压缩解压缩操做

能够参考http://blog.csdn.net/luoshengkim/article/details/46647423

一、tar.gz

       压缩、解压.tar.gz文件能够直接使用tarfile包,首先引入:import tarfile。解压缩操做以下:

tar = tarfile.open(path, 'r:gz') file_names = tar.getnames() for file_name in file_names: tar.extract(file_name, path) tar.close()

       压缩操做以下:

tar = tarfile.open(tarpath, 'w:gz') if isFile(srcpath): tar.add(srcpath, arcname=srcpath) elif isDir(srcpath): for root, dir, files in os.walk(srcpath): for file in files: fullpath = os.path.join(root, file) tar.add(fullpath, arcname=file) tar.close()

       tarfile.open的mode有如下种,每种对应不一样的方式,须要根据本身须要选取:

mode action
'r' or 'r:*' Open for reading with transparent compression (recommended). 'r:' Open for reading exclusively without compression. 'r:gz' Open for reading with gzip compression. 'r:bz2' Open for reading with bzip2 compression. 'a' or 'a:' Open for appending with no compression. The file is created if it does not exist. 'w' or 'w:' Open for uncompressed writing. 'w:gz' Open for gzip compressed writing. 'w:bz2' Open for bzip2 compressed writing.

二、gz

       压缩、解压.gz文件能够直接使用gzip包,首先引入:import gzip。解压缩操做以下:

fname = path.replace('.gz', '').replace('.GZ', '') gfile = gzip.GzipFile(path) open(fname, 'wb').write(gfile.read()) gfile.close()

       压缩操做以下:

gfile = gzip.GzipFile(srcpath + '.gz', mode='w') gfile.write(open(srcpath, 'rb').read()) gfile.close()

       此处一样须要注意mode的选取,而且还要注意解压缩的时候建立解压缩文件时的mode。

三、zip

       压缩、解压.zip文件能够直接使用zipfile包,首先引入:import zipfile。解压缩操做以下:

zip_file  = zipfile.ZipFile(path, mode='r') for name in zipfile.namelist(): zip_file.extract(name, getFilenameWithoutExt(path)) zip_file.close()

       压缩操做以下:

zip_file  = zipfile.ZipFile(zippath, mode='w') if isFile(srcpath): zip_file.write(srcpath, arcname=srcpath) elif isDir(srcpath): for root, dir, files in os.walk(srcpath): for file in files: fullpath = os.path.join(root, file) zip_file.write(fullpath, arcname=file) zip_file.close()

3、hdfs操做

       hdfs操做采用hdfs3库,这是c语言写的libhdfs库的python封装版,基本能知足经常使用的hdfs操做。

3.1 引入hdfs3

       只须要知道namenode的地址以及端口号便可,代码以下:

from hdfs3 import HDFileSystem hdfs = HDFileSystem(host='namenode', port=8020)

3.2 创建文件夹

       若是想要上传文件等到hdfs,必须保证其文件夹存在,不然会报错,此时就能够先建立文件夹,只须要使用hdfs.mkdir(dir)便可,而且此命令会递归建立文件夹,即不须要一层层的建立不存在的文件夹。

3.3 上传文件

       上传文件的时候只须要指定本地文件地址以及hdfs中存储地址便可,hdfs地址也须要包含文件名,命令为hdfs.put(localfile, remotefile)。

3.4 hdfs操做封装

       一样将我封装的hdfs操做代码封装以下:

def mkdir(remotepath): if not exists(remotepath): hdfs.mkdir(dir) def get(remotepath, localpath): if exists(remotepath): hdfs.get(remotepath, localpath) def put(localfile, remotefile): dir = getDirPath(remotefile) mkdir(dir) hdfs.put(localfile, remotefile) def exists(remotepath): return hdfs.exists(remotepath) def delete(remotepath): if exists(remotepath): hdfs.rm(remotepath, recursive=True)

4、总结

       本文简单总结了python的部分经常使用基础操做以及hdfs操做,最后还要说明一点,对这种非强类型的语言,在定义变量名称以及传入参数的时候必定要当心,不然会出现一些莫名其妙的错误。

相关文章
相关标签/搜索