文章内容
2024/7/22 2:01:37,作 者: 黄兵
Python 写文件示例
下面是一个 Python 写文件的示例:
import os
# 定义 token 内容
token_content = "your_token_here"
# 定义文件路径
file_path = "/root/info.txt"
# 检查文件是否存在
if os.path.exists(file_path):
# 如果文件存在,打开文件并写入 token 内容
with open(file_path, 'a') as file:
file.write(token_content + "\n")
else:
# 如果文件不存在,新建文件并写入 token 内容
with open(file_path, 'w') as file:
file.write(token_content + "\n")
这个代码片段将检查 /root 目录中的 info.txt 文件是否存在。如果存在,它会在文件末尾追加 token_content;如果不存在,则新建文件并写入 token_content。
运行这段代码的时候,需要确保 Python 脚本具有适当的权限来访问和写入 /root 目录中的文件
其它相关推荐:
评论列表