python获取本地文件内容
import os
def main():
# 定义文件路径,使用原始字符串来避免转义字符问题
file_path = r'F:\temp\test.txt'
# 检查文件是否存在
if not os.path.exists(file_path):
print(f"Error: The file at {file_path} does not exist.")
return
try:
# 打开并读取文件内容
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
print(content)
except IOError as e:
print(f"Error: An I/O error occurred: {e}")
if __name__ == '__main__':
main()

578

被折叠的 条评论
为什么被折叠?



