python模块引入即一些方法(未完待续……)

一、公共方法:

公共方法

1.查看内存地址: id(param)  ,如,e.g.1

    

l1 = [1,1,2,3,4,5,6,8]
print('print l1 memory address:',id(l1))#查看内存地址
l2 = [1,1,2,3,4,5,6,8]
print('print l2 memory address:',id(l2))#查看内存地址

2.










e.g.1


二、模块

模块

1.什么是模块

    模块就是一个包含Python代码的文本文件,对模块的主要需求就是要求文件名以.py结尾


2. copy 模块 e.g.1

    1)导入模块: import copy

          方法:copy.deepcopy(param)  #深copy

                     概念补充:浅copy,如:l1 = [1,2,3]   那么l2=l1 即属于浅copy即,l1和l2指向同一块内存空间

          练习:

          

l1 = [1,1,2,3,4,5,6,8]
l2 = copy.deepcopy(l1)
print(l2)

3.string 模块

   1)导入模块:import string

   2)方法(e.g.2):

       a.string.ascii_lowercase  #生成小写字母
       b.string.ascii_uppercase #生成大写字母
       c.string.digits              #生成数字
       d.string.ascii_letters      #生成字母(包括大小写)

       e.string.punctuation    #生成特殊字符

       ps.此处的字符都是英文半角符号

4.time 模块

   1)导入模块:import time

   2)方法(e.g.3)

        a.time.sleep(seconds)  # 延迟多少秒执行程序 如,time.sleep(60) 即暂停60秒,之后再执行程序,seconds可以为float类型

        b.time.date.today() #获取当前日期

        c.time.time()     # 返回从元年计时开始到现在的秒数,Return the current time in seconds since the Epoch

5.os模块

    1)概念;os是一个内置模块

    2)方法

        1>  获取/更改文件路径或目录

            print('获取父目录:',os.path.dirname(__file__) )  # 获取父目录

                  说明:

                    os.path.dirname(__file__)   #获取父目录,__file__代表获取当前文件目录

                    获取到的路径与当前系统路径分隔符不一致 ,需要使用绝对路径转一下

            print( os.path.abspath( __file__ ))  # 获取绝对路径          

                说明:

                    os.path.abspath( __file__ ) # 获取当前文件的绝对路径

                    可以把__file__换成其他的路径


            print('获取当前工作目录:',os.getcwd() )  # 取当前工作目录

            print('获取当前工作目录上级目录:',os.path.dirname(os.getcwd()))

            print('更改当前工作目录,到指定的目录,返回值为None,可以通过os.getcwd进行验证:',os.chdir("F:\lp_test\\besttest\\auto_test\homework\syz_automatic_code\day7"))  # 更改当前目录

            os.curdir   #获取当前目录的符号

            print('获取到当前目录的符号 >> "."  >> :  ',os.curdir) #打印出来是个.

            print('获取当前目录的绝对路径:',os.path.abspath(os.curdir))  # 当前目录

            print('获取父目录:',os.pardir )  # 父目录

            print('获取当前目录父目录的绝对路径:',os.path.abspath(os.pardir))  

            #os.walk  获取当前目录的路径(绝对路径),目录名,文件名

            目录树生成器,文件数树的每级目录都生成一个三元元组,元组元素为目录路径,目录名列表,文件名列表 

            # for data in os.walk('F:\lp_test\\besttest\\auto_test\homework\syz_automatic_code\day6'):
            #     print(data)
            # for abs_path,dirs,file in os.walk('F:\lp_test\\besttest\\auto_test\homework\syz_automatic_code\day6'):
            #     print(abs_path,dirs,file)
            

        2>获取系统路径分隔符

            #print('当前系统分隔符:',os.sep)  # 当前操作系统的路径分隔符

            #print('获取当前系统换行符:',os.linesep)  # 当前操作系统的换行符 window \r\n

        3>更改文件/目录访问权限

            os.chmod( "/usr/local", 7 )  # 给文件/目录加权限 适用于linux系统

        4>创建目录

            print( os.makedirs( "test2/test3" ) )  # 递归创建文件夹,父目录不存在时创建父目录 #已经存在FileExistsError: [WinError 183] 当文件已存在时,无法创建该文件。: 'test2/test3'

            print( os.mkdir( "test2/test6" ) )  # 创建文件夹  #创建多层目录,如果父目录不存在FileNotFoundError: [WinError 3] 系统找不到指定的路径。: 'test4/test5',目录以存在也会报错

        5>删除目录

            print( os.removedirs( "test2/test6" ) )  # 递归删除空目录,只能删除空目录,如果删除的目录不存在会报错,FileNotFoundError: [WinError 2] 系统找不到指定的文件。: 'test2/test6'

            print( os.rmdir( "test2" ) )  # 删除指定的文件夹  只能删除空的目录OSError: [WinError 145] 目录不是空的。: 'test2'

            print( os.remove( "test2/test5/123" ) )  # 删除文件 只能删除文件,不能删除文件夹 PermissionError: [WinError 5] 拒绝访问。: 'test2',如果删除的文件不存在会报异常,FileNotFoundError: [WinError 2] 系统找不到指定的文件。: 'test2/test5/123'

        6>获取目录的文件名及目录名

            print('将当前路径中的文件及文件夹放在一个list中:',os.listdir())  # 列出当前目录下所有的文件及文件夹放在一个list中

            print('当前目录的父目录:%s,包含的文件及文件夹list:%s'%(os.path.abspath(os.pardir),os.listdir(os.path.abspath(os.pardir))))  # 列出当前目录下所有的文件及文件夹放在一个list中

            os.scandir()     """ scandir(path='.') -> iterator of DirEntry objects for given path """ 为给定的路径生成一个DirEntry对象的迭代器

            print(':',os.scandir(os.path.abspath(os.path.dirname(__file__))))  返回的是一个一个Direntry的迭代器

        7>重命名

            os.rename( "liangpan.py", "lprename.py" )  # 重命名,将liangpan.py文件名重命名为lprename.py,如果文件名找不到会报异常,FileNotFoundError: [WinError 2] 系统找不到指定的文件。: 'liangpan.py' -> 'lprename.py'

        8>获取文件信息

            print('获取文件信息:',os.stat( "lprename.py" ) )  # 获取文件信息

            # print( os.path.getatime( "len_os.py" ) )  # 输出最近访问时间

            os.path.getsize()    #获取文件大小,常用

            size = os.path.getsize('day6_2_new.py')  #获取文件大小单位B

            print('文件大小:%s bytes'%size)

        9>获取系统分隔符

            print('当前系统分隔符:',os.sep)  # 当前操作系统的路径分隔符
            print('获取当前系统换行符:',os.linesep)  # 当前操作系统的换行符 window \r\n
            print('当前系统环境变量的分隔符:',os.pathsep )  # 当前系统的环境变量中每个路径的分隔符,linux是:,windows是;

            print('当前系统的环境变量:',os.environ )  # 当前系统的环境变量,返回的是一个元组,元组中是一个字典

        10>判断

            os.path.isfile(path)  #传入的路径是否为一个常规的文件,常用         print( os.path.isfile( "/usr/local" ) )  # 判断是否是一个文件

        






e.g.1copy方法解释
def deepcopy(x, memo=None, _nil=[]):
    """Deep copy operation on arbitrary Python objects.

    See the module's __doc__ string for more info.
    """

    if memo is None:
        memo = {}

    d = id(x)
    y = memo.get(d, _nil)
    if y is not _nil:
        return y

    cls = type(x)

    copier = _deepcopy_dispatch.get(cls)
    if copier:
        y = copier(x, memo)
    else:
        try:
            issc = issubclass(cls, type)
        except TypeError: # cls is not a class (old Boost; see SF #502085)
            issc = 0
        if issc:
            y = _deepcopy_atomic(x, memo)
        else:
            copier = getattr(x, "__deepcopy__", None)
            if copier:
                y = copier(memo)
            else:
                reductor = dispatch_table.get(cls)
                if reductor:
                    rv = reductor(x)
                else:
                    reductor = getattr(x, "__reduce_ex__", None)
                    if reductor:
                        rv = reductor(4)
                    else:
                        reductor = getattr(x, "__reduce__", None)
                        if reductor:
                            rv = reductor()
                        else:
                            raise Error(
                                "un(deep)copyable object of type %s" % cls)
                if isinstance(rv, str):
                    y = x
                else:
                    y = _reconstruct(x, memo, *rv)

    # If is its own copy, don't memoize.
    if y is not x:
        memo[d] = y
        _keep_alive(x, memo) # Make sure x lives at least as long as d
    return y

_deepcopy_dispatch = d = {}
e.g.2string方法
import string
print('lowercase:',string.ascii_lowercase)  #打印小写字母
print('uppercase:',string.ascii_uppercase)  #打印大写字母
print('digits:',string.digits)           #打印数字
print('letters:',string.ascii_letters)    #大写小写字母
print('punctuation:',string.punctuation)      #特殊字符
e.g.3 time模块方法

1.time.sleep

def sleep(seconds): # real signature unknown; restored from __doc__
    """
    sleep(seconds)
    
    Delay execution for a given number of seconds.  The argument may be
    a floating point number for subsecond precision.

    """
















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值