zoukankan      html  css  js  c++  java
  • day-18模块使用

    1.时间模块

    time:时间

    时间戳(timestamp):time.time()
    延迟线程的运行:time.sleep(secs)
    (指定时间戳下的)当前时区时间:time.localtime([secs])
    (指定时间戳下的)格林威治时间:time.gmtime([secs])
    (指定时间元组下的)格式化时间:time.strftime(fmt[,tupletime])

    ==============================================

    %y 两位数的年份表示(00-99)
    %Y 四位数的年份表示(000-9999)
    %m 月份(01-12)
    %d 月内中的一天(0-31)
    %H 24小时制小时数(0-23)
    %I 12小时制小时数(01-12)
    %M 分钟数(00=59)
    %S 秒(00-59)
    %a 本地简化星期名称
    %A 本地完整星期名称
    %b 本地简化的月份名称
    %B 本地完整的月份名称
    %c 本地相应的日期表示和时间表示
    %j 年内的一天(001-366)
    %p 本地A.M.或P.M.的等价符
    %U 一年中的星期数(00-53)星期天为星期的开始
    %w 星期(0-6),星期天为星期的开始
    %W 一年中的星期数(00-53)星期一为星期的开始
    %x 本地相应的日期表示
    %X 本地相应的时间表示
    %Z 当前时区的名称
    %% %号本身

    datatime:可以运算的时间

    当前时间:datetime.datetime.now()
    昨天:datetime.datetime.now() + datetime.timedelta(days=-1)
    修改时间:datatime_obj.replace([...])
    格式化时间戳:datetime.date.fromtimestamp(timestamp)

    2.系统模块

    sys:系统

    命令行参数List,第一个元素是程序本身路径:sys.argv
    退出程序,正常退出时exit(0):sys.exit(n)
    获取Python解释程序的版本信息:sys.version
    最大int值:sys.maxsize | sys.maxint
    环境变量:sys.path
    操作系统平台名称:sys.platform

    os:操作系统

    生成单级目录:os.mkdir('dirname')
    生成多层目录:os.makedirs('dirname1/.../dirnamen2')
    重命名:os.rename("oldname","newname")
    工作目录:os.getcwd()
    删除单层空目录:os.rmdir('dirname')
    移除多层空目录:os.removedirs('dirname1/.../dirnamen')
    列举目录下所有资源:os.listdir('dirname')
    路径分隔符:os.sep
    行终止符:os.linesep
    文件分隔符:os.pathsep
    操作系统名:os.name
    操作系统环境变量:os.environ
    执行shell脚本:os.system()

    os.path:系统路径操作

    执行文件的当前路径:__file__
    返回path规范化的绝对路径:os.path.abspath(path)
    将path分割成目录和文件名二元组返回:os.path.split(path)
    上一级目录:os.path.dirname(path)
    最后一级名称:os.path.basename(path)
    指定路径是否存在:os.path.exists(path)
    是否是绝对路径:os.path.isabs(path)
    是否是文件:os.path.isfile(path)
    是否是路径:os.path.isdir(path)
    路径拼接:os.path.join(path1[, path2[, ...]])
    最后存取时间:os.path.getatime(path)
    最后修改时间:os.path.getmtime(path)
    目标大小:os.path.getsize(path)

    normcase函数
    在Linux和Mac平台上,该函数会原样返回path,在windows平台上会将路径中所有字符转换为小写,并将所有斜杠转换为饭斜杠。
    >>> os.path.normcase('c:/windows\system32\')
    'c:\windows\system32\'

    normpath函数
    规范化路径,如..和/
    >>> os.path.normpath('c://windows\System32\../Temp/')
    'c:\windows\Temp'

    >>> a='/Users/jieli/test1/\a1/\\aa.py/../..'
    >>> print(os.path.normpath(a))
    /Users/jieli/test1

    calendar:日历

    判断闰年:calendar.isleap(year)
    查看某年某月日历:calendar.month(year, mouth)
    查看某年某月起始星期与当月天数:calendar.monthrange(year, mouth)
    查看某年某月某日是星期几:calendar.weekday(year, month, day)

    跨文件夹移动文件

    import os
    import sys
    BASE_DIR = os.path.dirname(os.path.dirname(__file__))
    sys.path.append(BASE_DIR)
    
    def move_file(file, folder):
        if not (os.path.exists(file) and os.path.isfile(file)):
            print('文件不存在或非法')
            return False
        if not os.path.exists(folder):
            os.makedirs(folder)
        file_name = os.path.split(file)[1]
        # file_name = os.path.basename(file)
        new_file = os.path.join(folder, file_name)
    
        with open(file, 'rb') as rf, open(new_file, 'wb') as wf:
            for line in rf:
                wf.write(line)
    
        os.remove(file)
    
    # 将目标文件夹下的目标文件移动到指定文件夹下
    file = os.path.join(BASE_DIR, 'part5', 'mm.py')
    folder = os.path.join(BASE_DIR, 'part6', 'abc')
    move_file(file, folder)

    递归删除的思路

    def delete_dir(folder):
        for path in os.listdir(folder):
            # 如果path是文件夹 delete_dir(path)
            # 如果是文件os.remove(path)
            pass
        # for走完了代表folder内部删空了,可以删folder

    递归遍历打印目标路径中所有的txt文件

    def print_txt(folder):
        if not os.path.exists(folder) or os.path.isfile(folder):
            return
        for path in os.listdir(folder):
            file_path = os.path.join(folder, path)
            if os.path.isfile(file_path) and file_path.endswith('.txt'):
                print(path)
            elif os.path.isdir(file_path):
                print_txt(file_path)  # 递归
    
    
    target_path = os.path.join(BASE_DIR, 'part6', 'target')
    print_txt(target_path)

    项目开放周期

    '''
    1.调研
    2.需求分析
    3.架构师完成项目demo,完成项目架构
    4.分工
    5.写代码
    6.白盒黑盒测试
    7.项目审核发布 => 项目 -> 产品
    '''

    '''
    bin: 可执行文件,入口,入口也可以放在项目根目录下
    core: 核心代码
    db:数据库相关文件
    interface:接口
    lib:包、模块、第三方文件夹
    log:日志
    setting:配置
    static:静态文件
    '''

     

  • 相关阅读:
    Detect loop in a singly linked list
    Partition an array around an interger
    Binary search for the first element greater than target
    Searching in a rotated and sorted array
    where, group by, having
    [scalability] Find all documents that contain a list of words
    [DP] 堆盒子问题
    cocos2dx 内存管理的理解
    cocos2dx 2.x版本:简化提炼tolua++绑定自定义类到lua中使用
    OpenGl从零开始之坐标变换(下)
  • 原文地址:https://www.cnblogs.com/klw1/p/10827546.html
Copyright © 2011-2022 走看看