python实用小工具

1.Python实现读文件

def read_file(file_path):
    with open(filepath) as f:
        content = f.read()
    return content

2.Python写文件

def write_file(file_path):
    need_file_str = f"""想写入的文件内容"""
    with open(file_path, 'w+') as f:
        f.write(need_file_str)


def write_file(str):
    need_to_save_path=f'./pythonProjectTest/otherInfo/recvInfo/1.txt'
    with open(need_to_save_path, 'w+') as f:
        f.write(str)

3.Python执行命令行操作

def execute_command(command):
    res = os.popen(command).read()
    print(f'执行{command}成功\n{res}')
    return res

例如调用如下,就是查看当前路径

execute_command(pwd)

5.拼接list中全部字符串例子:

list=["a"]
list.append("b")
print(list)   #结果为['a','b']
ding = "*".join(list)  # 这里表示用*分隔符连接列表元素,可以自行选择
print(ding)  #结果就是“a*b”

6.指定定时任务

import schedule
import time

def print_time():
	print(time.time())
	
if __name__ == '__main__':
    print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(time.time()))))
    schedule.every(1).minutes.do(print_time)               # 每隔 10 分钟运行一次 print_time 函数
    schedule.every().hour.do(print_time)                    # 每隔 1 小时运行一次 print_time 函数
    schedule.every().day.at("10:00").do(print_time)         # 每天在 10:00 时间点运行 print_time 函数
    schedule.every().monday.do(print_time)                  # 每周一 运行一次 print_time 函数
    schedule.every().wednesday.at("13:15").do(print_time)   # 每周三 13:15 时间点运行 print_time 函数
    schedule.every().minute.at(":17").do(print_time)        # 每分钟的 17 秒时间点运行 print_time 函数
    while True:
        schedule.run_pending()
        time.sleep(1)

1.get请求

在这里插入代码片

你可能感兴趣的:(语言学习,c语言,objective-c,ios)