Skip to content

Instantly share code, notes, and snippets.

@668168
Created September 22, 2023 13:28
Show Gist options
  • Save 668168/5a08749902117d534ce25f9abda17299 to your computer and use it in GitHub Desktop.
Save 668168/5a08749902117d534ce25f9abda17299 to your computer and use it in GitHub Desktop.
读取文件内容
def read_file_from_current_path(filename):
# 显示当前路径
current_path = os.getcwd()
print(f"当前路径是:{current_path}")
# 检查文件是否存在
file_path = os.path.join(current_path, filename) # 也可以直接使用文件名,如果文件在当前工作目录下
if os.path.exists(file_path):
print(f"{file_path} 存在。")
else:
print(f"{file_path} 不存在。")
with open(file_path, 'r', encoding='utf-8') as fb:
# Read the entire file content
file_string = fb.read()
return file_string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment