zoukankan      html  css  js  c++  java
  • 时间操作和文件操作

    1.time模块

    import time
    time.time() #当前时间戳
    tuple_time = time.localtime() #转换时间戳为元组
    stamp_time = time.mktime(time.localtime()) #给定时间元组转为时间戳
    time_change = time.strftime('%Y-%m-%d %H:%M:%S:%p',time.localtime()) #格式化显示(时间元组相互转换)
    now = time.strftime("%Y年%m月%d日%H时%M分%S秒", time.localtime(time.time())) #格式化显示当前时间

    2.datetime模块

    import datetime
    time = datetime.datetime.now()  #获取当前时间
    time2 = datetime.datetime.today() #获取当前时间
    time.timestamp() #获取时间戳
    time.strftime('%Y-%m-%d %H:%M:%S') # 格式化输出日期
    time.year   #获取年
    time.month
    3、有两个文件a.txt,b.tx,把他们中的内容进行互换
    方法一:
    file_a = open('C:\Users\liangyq\Desktop\a.txt','r')
    file_b = open('C:\Users\liangyq\Desktop\b.txt','r')
    content_a = file_a.read()
    content_b = file_b.read()
    print(content_a,content_b)
    file_b.close()
    file_a.close()
    file_a = open('C:\Users\liangyq\Desktop\a.txt','w') # 以覆盖方式打开
    file_b = open('C:\Users\liangyq\Desktop\b.txt','w')
    file_a.write(content_b)
    file_a.flush()
    file_b.write(content_a)
    file_b.flush()
    file_b.close()
    file_a.close()

    方法二:

    import os
    #交换两个文件名字
    os.rename('C:\Users\liangyq\Desktop\a.txt','C:\Users\liangyq\Desktop\b.txt') # (old_file_name,new_file_name)
    os.rename('C:\Users\liangyq\Desktop\test\b.txt','C:\Users\liangyq\Desktop\test\a.txt')
    4、用循环的方式新建100个文件,文件编号为001--100,文件内容显示创建时间
    import datetime
    for i in range(1,101):
        file_number = '%.3d.txt' % i #格式化:不满3位数从左边补0
        file_name = 'C:\Users\liangyq\Desktop\test\' + file_number
        file_create = open(file_name,'w+')
        file_create_time = str(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')) # 自定义创建时间
        print(file_create_time)
        file_create.write(file_create_time)
        file_create.flush()
        file_create.close()
    文件的操作:
    file_object = open('C:\Users\liangyq\Desktop\test\a.txt','r+')
    
    list3 = file_object.readline(2)  #读一行,如果定义了size,有可能返回的只是一行的一部分
    list4 = file_object.readlines() #把文件每一行作为一个list的一个成员,并返回这个list。其实它的内部是通过循环调用readline()来实现的。如果提供size参数,size是表示读取内容的总长,也就是说可能只读到文件
    list5 = '
    msnsnsnnsnsnsns'
    file_object.writelines(list5)
    file_object.seek(0)   #移动指针所在位置,移动到最前面位置
    list6 =file_object.readlines()
    a = file_object.name
    b = file_object.encoding
    c = file_object.mode #文件打开的模式
    file_object.seek(0)   #移动指针所在位置,移动到最前面位置
    d = file_object.tell() #文件指针所在位置
    list7 = next(file_object)
    file_object.close()


    5.excel操作
      读取一个excel表格单元格的内容:
    from openpyxl import load_workbook
    #打开文件:
    excel = load_workbook('C:\Users\liangyq\Desktop\test\test.xlsx')
    #获取sheet:
    table  = excel['sheet1'] #通过表名获取
    C4_data = table.cell(4,3).value#获取表格内容,是从第一行第一列是从1开始的,注意不要丢掉 .value (列,行)
    print(C4_data)

          往excel写入数据

       for col in range(1,len(book_info.keys())+1):
                active_sheet.cell(row=len(book_infos)+1, column=col, value=list(book_info.values())[col - 1])
        wb.save('bookinfos.xlsx') #文件打开时 不能操作
        return  book_infos

     yaml

    https://www.jianshu.com/p/eaa1bf01b3a6

  • 相关阅读:
    upstream实现内网网站在公网访问
    ifconfig筛选出IP
    ansible安装及配置
    ansible puppet saltstack三款自动化运维工具的对比
    upstream(负载均衡)
    nginx反代及后端web配置
    centos7 安装gdb (调试nginx)
    centos 7搭建 strongSwan
    MySQL主从及读写分离配置
    Python中的用open打开文件错误,FileNotFoundError: [Errno 2] No such file or directory:
  • 原文地址:https://www.cnblogs.com/joy-field/p/12925535.html
Copyright © 2011-2022 走看看