• 文件读写

    • 源文件test.txt
    • line1
      line2
      line3
    • 读取文件内容
    •  f = open('./test.txt','r')#只读模式打开test.txt文件
      print(f.read())#读取文件的所有内容
      f.close()#关闭文件
    • 读取文件内容(安全读取try--finally)
    •  try:#添加try异常捕获
      f = open('./test.txt','r')#只读模式打开test.txt文件
      print(f.read())#读取文件的所有内容
      finally:
      if f:
      f.close()#关闭文件
    • 使用with简化读取代码
    •  with open('./test.txt','r') as f:
      print(f.read())
    • 以上所有的执行结果均为
    • ➜  Python  python3 readTxt.py
      line1
      line2
      line3
    • 读取方法详解
      • read():读取文件的所有内容。针对小文件
      • read(size):按指定大小来读取文件的内容。size字节大小。针对大文件
      • readlines():按行来读取文件的所有内容,返回为list格式。针对配制文件
    • 读取模式
      • 'r':读文件
      • 'rb':二进制读文件
      • 'w':写文件
      • 'wb':二进制写文件
  • StringIO、BytesIO
    • StringIO:字符串IO
      1. 先从io中引入StringIO
      2. 创建一个StringIO对象
      3. 写字符串到StringIO对象f中
      4. 获取字符串内容f.getvalue()
      • >>> from io import StringIO
        >>> f = StringIO()
        >>> f.write('hello')
        5
        >>> f.write(' ')
        1
        >>> f.write('world!')
        6
        >>> print(f.getvalue())
        hello world!
    • BytesIO
      1. 从io中引入BytesIO
      2. 创建一个BytesIO对象
      3. 写字节对象
      4. 获取写入的字节内容内容
      • >>> from io import BytesIO
        >>> f = BytesIO()
        >>> f.write('我是中文'.encode('utf-8'))
        12
        >>> print(f.getvalue())
        b'\xe6\x88\x91\xe6\x98\xaf\xe4\xb8\xad\xe6\x96\x87'
  • 多进程
    • fork()

      • fork()调用一次,返回两次,因为操作系统自动把当前进程(称为父进程)复制了一份(称为子进程),然后,分别在父进程和子进程内返回。
      • 子进程永远返回0,而父进程返回子进程的ID。这样做的理由是,一个父进程可以fork出很多子进程,所以,父进程要记下每个子进程的ID,而子进程只需要调用getppid()就可以拿到父进程的ID。
      •  import os
        
         print('Process (%s) starting...' % os.getpid())
        
         pid = os.fork()
        
         if pid == 0 :
        print('I am child process (%s) and my parent is %s' %(os.getpid(),os.getppid()))
        else:
        print('I (%s) just created a child process (%s)' %(os.getpid(),pid))
      • ➜  Python  python3 ThreadDemo.py
        Process (711) starting...
        I (711) just created a child process (712)
        I am child process (712) and my parent is 711
    • multiprocessing-Process
      •  from multiprocessing import Process
        import os #子进程代码
        def run_proc(name):
        print('Run child process %s (%s).'%(name,os.getpid())) if __name__ == '__main__':
        print('Parent process %s.' % os.getpid())
        p = Process(target=run_proc,args = ('test',))
        print('Child process will start..')
        #启动子进程
        p.start()
        #等待子进程结束后再继续往下运行
        p.join()
        print('Child process end.')
      • ➜  Python  python3 MultiProcessins.py
        Parent process 718.
        Child process will start..
        Run child process test (719).
        Child process end.
    • Pool线程池
      •  from multiprocessing import Pool
        import os,time,random #子进程代码
        def long_time_task(name):
        print('Run task %s (%s).' %(name,os.getpid()))
        start = time.time()
        time.sleep(random.random()*3)
        end = time.time()
        print('Task %s runs %.2f seconds.' %(name,(end-start))) if __name__ == '__main__':
        print('Parent process %s.'%os.getpid())
        #创建线程池
        p = Pool(4)
        for i in range(5):
        p.apply_async(long_time_task,args=(i,))
        print('Waiting for all subprocesses done..')
        p.close()
        p.join()
        print('All subprocesses done.')
      • ➜  Python  python3 Pool.py
        Parent process 730.
        Waiting for all subprocesses done..
        Run task 0 (731).
        Run task 1 (732).
        Run task 2 (733).
        Run task 3 (734).
        Task 2 runs 0.18 seconds.
        Run task 4 (733).
        Task 3 runs 0.83 seconds.
        Task 0 runs 1.18 seconds.
        Task 4 runs 2.46 seconds.
        Task 1 runs 2.66 seconds.
        All subprocesses done.
    • 子进程
      • 调用外部进程(系统进程):nslookup
      • 使用子进程
      •  import subprocess
        
         print('$ nslookup www.cnblogs.com')
        #调用外部
        r = subprocess.call(['nslookup','www.cnblogs.com'])
        print('Exit code:',r)
        ➜  Python  python3 SubProcess.py
        $ nslookup www.cnblogs.com
        Server: 10.1.1.5
        Address: 10.1.1.5#53 Non-authoritative answer:
        Name: www.cnblogs.com
        Address: 42.121.252.58 Exit code: 0
      • 直接使用nslookup查看结果
      • ➜  ~  nslookup www.cnblogs.com
        Server: 10.1.1.5
        Address: 10.1.1.5#53 Non-authoritative answer:
        Name: www.cnblogs.com
        Address: 42.121.252.58
    • 多进程数据通信
      • 一个向Queue中写数据,另一外读数据
      •  from multiprocessing import Process,Queue
        import os,time,random #写数据
        def write(q):
        print('Process to write:%s'%os.getpid())
        for value in ['A','B','C']:
        print('Put %s to queue.'%value)
        q.put(value)
        time.sleep(random.random()) #读数据
        def read(q):
        print('Process to read:%s'%os.getpid())
        while True:
        value = q.get(True)
        print('Get %s from queue.'%value) if __name__ == '__main__':
        q = Queue()
        pw = Process(target = write,args=(q,))
        pr = Process(target = read,args=(q,))
        pw.start()
        pr.start()
        pw.join()
        pr.terminate()
      • ➜  Python  python3 ProcessConn.py
        Process to write:803
        Put A to queue.
        Process to read:804
        Get A from queue.
        Put B to queue.
        Get B from queue.
        Put C to queue.
        Get C from queue.
    • 选择
      • Unix/Linux下可使用fork()
      • 跨平台使用multiprocessing
      • 多进程数据通信Queue、Pipes
  • 多线程
    • 进程是由若干线程组成的,一个进程至少有一个线程。

      •  import time,threading
        
         #线程代码
        def loop():
        print('thread %s is running..'%threading.current_thread().name)
        n = 0
        while n < 5:
        n = n + 1
        print('thread %s >>> %s' %(threading.current_thread().name,n))
        time.sleep(1)
        print('thread %s ended.'%threading.current_thread().name) print('thread %s is running.'%threading.current_thread().name)
        t = threading.Thread(target = loop,name = 'LoopThread')
        t.start()
        t.join()
        print('thread %s ended.'%threading.current_thread().name)
      • ➜  Python  python3 Thread.py
        thread MainThread is running.
        thread LoopThread is running..
        thread LoopThread >>> 1
        thread LoopThread >>> 2
        thread LoopThread >>> 3
        thread LoopThread >>> 4
        thread LoopThread >>> 5
        thread LoopThread ended.
        thread MainThread ended.

Python3学习(3)-高级篇的更多相关文章

  1. Python3学习(2)-中级篇

    Python3学习(1)-基础篇 Python3学习(2)-中级篇 Python3学习(3)-高级篇 切片:取数组.元组中的部分元素 L=['Jack','Mick','Leon','Jane','A ...

  2. Python3学习(1)-基础篇

    Python3学习(1)-基础篇 Python3学习(2)-中级篇 Python3学习(3)-高级篇 安装(MAC) 直接运行: brew install python3 输入:python3 --v ...

  3. 数据库MySQL学习笔记高级篇

    数据库MySQL学习笔记高级篇 写在前面 学习链接:数据库 MySQL 视频教程全集 1. mysql的架构介绍 mysql简介 概述 高级Mysql 完整的mysql优化需要很深的功底,大公司甚至有 ...

  4. DP动态规划学习笔记——高级篇上

    说了要肝的怎么能咕咕咕呢? 不了解DP或者想从基础开始学习DP的请移步上一篇博客:DP动态规划学习笔记 这一篇博客我们将分为上中下三篇(这样就不用咕咕咕了...),上篇是较难一些树形DP,中篇则是数位 ...

  5. Storm学习笔记——高级篇

    1. Storm程序的并发机制 1.1 概念 Workers (JVMs): 在一个物理节点上可以运行一个或多个独立的JVM 进程.一个Topology可以包含一个或多个worker(并行的跑在不同的 ...

  6. vim 命令学习(高级篇)

    [1]打开文件方式 (1)vim +n filename 作用:打开文件,并定位到第n行 例如:vim +103 2019-02-26-errorrepeat.txt 效果:打开2019-02-26- ...

  7. 「 MySQL高级篇 」MySQL索引原理,设计原则

    大家好,我是melo,一名大二后台练习生,大年初三,我又来充当反内卷第一人了!!! 专栏引言 MySQL,一个熟悉又陌生的名词,早在学习Javaweb的时候,我们就用到了MySQL数据库,在那个阶段, ...

  8. 「MySQL高级篇」MySQL索引原理,设计原则

    大家好,我是melo,一名大二后台练习生,大年初三,我又来充当反内卷第一人了!!! 专栏引言 MySQL,一个熟悉又陌生的名词,早在学习Javaweb的时候,我们就用到了MySQL数据库,在那个阶段, ...

  9. 【原创 深度学习与TensorFlow 动手实践系列 - 4】第四课:卷积神经网络 - 高级篇

    [原创 深度学习与TensorFlow 动手实践系列 - 4]第四课:卷积神经网络 - 高级篇 提纲: 1. AlexNet:现代神经网络起源 2. VGG:AlexNet增强版 3. GoogleN ...

随机推荐

  1. path和classpath

    对于Java的初学者,这两个环境变量,总是要遇到的.这里做一下总结. 1.path和classpath的含义 path是Windows操作系统的一个环境变量. 当操作系统需要运行一个程序,它需要知道该 ...

  2. Karma:2. 集成 Karma 和 mocha 进行单元测试

    上一篇文章讨论了如何集成 Karma 和 Jasmine,地址见:Karma:1. 集成 Karma 和 Jasmine 进行单元测试 这篇文章讨论如何 Karma 集成 mocha 测试框架. 安装 ...

  3. IQ推理:红眼睛和蓝眼睛

    题目:  有一个很古老的村子,这个村子的人分两种,红眼睛和蓝眼睛,这两种人并没有什么不同,小孩在没生出来之前,没人知道他是什么颜色的眼睛,这个村子中间有一个广 场,是村民们聚集的地方,现在这个村子只有 ...

  4. Squid服务日志分析

    Squid服务日志分析 Apache 和 Squid 是两种著名的代理缓存软件,但Squid 较 Apache 而言是专门的代理缓存服务器软件,其代理缓存的功能强大,支持 HTTP/1.1 协议,其缓 ...

  5. Application tried to present a nil modal view controller on target “Current View Controller”解决方案

    情景再现 1,自定义一个storyboard: 打开xcode,按下cmd+N,新建一个Storyboard--->next 将新建立的storyboard命名为:TestViewControl ...

  6. 登陆mysql时出现unknown variable 'character_set_client=UTF8' 的错误

    今天,登陆数据库服务器的时候,出现了下面的错误: [root@localhost app]# mysql -uroot -p mysql: unknown variable 'character-se ...

  7. TJI读书笔记13-内部类

    TJI读书笔记13-内部类 TJI读书笔记13-内部类 创建内部类 内部类和外部类的关系 .this和.new 内部类和向上转型 局部内部类 匿名内部类 匿名内部类的定义和初始化 使用匿名内部类来实现 ...

  8. TS 流的解码过程(系摘抄)

    TS 流解码过程: 1. 获取TS中的PAT 2. 获取TS中的PMT 3. 根据PMT可以知道当前网络中传输的视频(音频)类型(H264),相应的PID,PCR的PID等信息. 4. 设置demux ...

  9. Android-ConvenientBanner轻松实现广告头效果

    Android-ConvenientBanner通用的广告栏控件,让你轻松实现广告头效果.支持无限循环, 可以设置自动翻页和时间(而且非常智能,手指触碰则暂停翻页,离开自动开始翻页. 你也可以设置在界 ...

  10. Selenium2+python自动化30-引入unittest框架

    from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.com ...