python读写文件与全局变量的使用

python记录时间

python读写文件与全局变量的使用,创建日志文件,记录时间

 

 

 

复制代码
# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.

import time

gl_log_file_name = ''

def add_to_log_func(info):
    global gl_log_file_name
    gl_log_file_name = time.strftime('%Y%m%d', time.localtime())+'.txt'
    f = open(gl_log_file_name, 'a')  # 'a'追加模式
    # current_time = time.time()
    # print(current_time)
    # print(time.localtime())
    time2 = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
    #print(time2)
    f.write(time2+' '+info+' \n')
    f.close()
def read_file_func():
    global gl_log_file_name
    print('当前文件名:',  gl_log_file_name)
    f = open(gl_log_file_name, 'r')
    read_content = f.read()
    #print('读取内容:', read_content)
    f.close()
    return read_content

def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    #print_hi('PyCharm')
    add_to_log_func('abcdefg')
    print("read_content:\n", read_file_func())
    msg = input()


# See PyCharm help at https://www.jetbrains.com/help/pycharm/
复制代码

 

posted @   txwtech  阅读(41)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
历史上的今天:
2022-12-29 c# 启动代码无法打开照片-Win10企业版LTSC图片打开方式里没有默认照片查看器
2019-12-29 c++ cc24a_demo //转换函数,用来做转换操作符,int()括号里面必须是空的,必须定义为const,代码示范
2019-12-29 VS2017未安装MFC解决方法
2019-12-29 program files (x86)\microsoft visual studio 14.0\vc\include\xtree,如果没有找到,下标溢出了,就报错咯
点击右上角即可分享
微信分享提示