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)
3.Python执行命令行操作
def execute_command(command):
res = os.popen(command).read()
print(f'执行{command}成功\n{res}')
return res
例如调用如下,就是查看当前路径
execute_command(pwd)
4.Python上传文件
def upload_file_url(filepath):
try:
f = open(filepath, 'rb')
files = {'image': f}
with requests.Session() as s:
res = s.post('上传的地址', files=files)
res = json.loads(res.text)
except:
print("上传失败")
sendmessage(f"上传文件失败{res.text}")
res = {
'url': ''
}
return res['url']
5.拼接list中全部字符串例子:
list=["a"]
list.append("b")
print(list)
ding = "*".join(list)
print(ding)
|