首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往
  • 您找到你想要的搜索结果了吗?
    是的
    没有找到

    Python 中的tab补全

    1.准备一个Python脚本 cat > tab.py <<EOF #!/usr/local/bin/python # python tab file import sys import readline import rlcompleter import atexit import os # tab completion readline.parse_and_bind('tab: complete') # history file histfile = os.path.join(os.environ['HOME'], '.pythonhistory') try:     readline.read_history_file(histfile) except IOError:     pass atexit.register(readline.write_history_file, histfile) del os, histfile, readline, rlcompleter EOF 2.查看Python默认的模块存放位置 [root@victor python2.7]# python Python 2.7.6 (default, Sep 17 2017, 04:41:33) [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> sys.path ['', '/usr/local/lib/python27.zip', '/usr/local/lib/python2.7', '/usr/local/lib/python2.7/plat-linux2', '/usr/local/lib/python2.7/lib-tk', '/usr/local/lib/python2.7/lib-old', '/usr/local/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/site-packages'] 3.拷贝到目录下 cp tab.py /usr/local/lib/python2.7 4.现在可以用了 [root@victor python]# cp tab.py /usr/local/lib/python2.7 [root@victor python]# python Python 2.7.6 (default, Sep 17 2017, 04:41:33) [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import tab >>> import sys >>> sys. sys.__class__(              sys.__sizeof__(             sys.displayhook(            sys.getprofile(             sys.ps1 sys.__delattr__(            sys.__stderr__              sys.dont_write_bytecode     sys.getrecursionlimit(      sys.ps2 sys.__dict__                sys.__stdin__               sys.exc_clear(              sys.getrefcount(            sys.py3kwarning sys.__displayhook__(        sys.__stdout__              sys.exc_info(               sys.getsizeof(              sys.setcheckinterval( sys.__doc__                 sys.__str__(                sys.exc_type                sys.gettrace(               sys.setdlopenflags( sys.__excepthook__(         sys.__subclasshook__(       sys.excepthook(             sys.hexversion

    03

    python对大文件的增量读取

    对于很多大文件的增量读取,如果遍历每一行比对历史记录的输钱或者全都加载到内存通过历史记录的索引查找,是非常浪费资源的,网上有很多人的技术博客都是写的用for循环readline以及一个计数器去增量读取,这样是十分脑残的,假如文件很大,遍历一次太久。  我们需要了解获取文件句柄的基本理论,其中包含的指针操作等。  原理是这样子,linux的文件描述符的struct里有一个f_pos的这么个属性,里面存着文件当前读取位置,通过这个东东经过vfs的一系列映射就会得到硬盘存储的位置了,所以很直接,很快。  以下是利用python实战代码,核心函数tell(),seek(). 也是调用的系统调用seek tell seek()的三种模式:    (1)f.seek(p,0)  移动当文件第p个字节处,绝对位置    (2)f.seek(p,1)  移动到相对于当前位置之后的p个字节    (3)f.seek(p,2)  移动到相对文章尾之后的p个字节 tell():    返回当前文件的读取位置。 代码: #!/usr/bin/python fd=open("test.txt",'r') #获得一个句柄 for i in xrange(1,3): #读取三行数据    fd.readline() label=fd.tell() #记录读取到的位置 fd.close() #关闭文件 #再次阅读文件 fd=open("test.txt",'r') #获得一个句柄 fd.seek(label,0)# 把文件读取指针移动到之前记录的位置 fd.readline() #接着上次的位置继续向下读取 后续:今儿有一人问我如何得知这个大文件行数,以及变化,我的想法是 方法1: 可以去遍历'\n'字符。 方法2: 从一开始就用for循环fd.readline()进行计数,然后变化的部分(用上文说的seek、tell函数做)再用for循环fd.readline()进行统计增加行数。

    01
    领券