# 打开文件,读取内容 f = open("example.txt", "r") content = f.read() print(content) f.close()
# 读取前10个字符 f = open("example.txt", "r") content = f.read(10) print(content) f.close()
# 写入文件 f = open("example.txt", "w") f.write("Hello, World!") f.close()
# 追加内容到文件末尾 f = open("example.txt", "a") f.write("\nNew content!") f.close()
# 使用with语句自动关闭文件 with open("example.txt", "r") as f: content = f.read() print(content)
# 获取当前工作目录 import os print(os.getcwd())
# 连接两个路径 import os path1 = os.path.abspath("example.txt") path2 = os.path.join(os.getcwd(), "example.txt") print(path1) print(path2)
标签: sqlite