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/
欢迎讨论,相互学习。
cdtxw@foxmail.com