Files
Python_CookBook_repo/5.文件与IO/2.将输出重定向到文件中.py
2025-09-10 16:12:45 +08:00

11 lines
380 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

if __name__ == "__main__":
# 如果想要将东西输出到文件中可以在print函数中指定file字段
# print函数的默认结尾是\n如果想要替换这个默认结尾可以在end字段设置
path = r"5.文件与IO/2.somefile.txt"
with open(path, "at") as f:
for i in range(10):
print(i, file=f, end=' ')
print("Done")