비밀 공개
이 단계에서는 파일을 쓰고 닫는 Python 스크립트를 생성하여 새로운 지식을 보여주고, 사원 안의 지혜를 보존할 것입니다.
- 동일한 디렉토리에서
write_file.py라는 Python 스크립트 파일을 엽니다.
write_file.py 파일에 다음 코드를 작성합니다.
## write_file.py
file_path = '/home/labex/project/ancient_wisdom.txt'
with open(file_path, 'w') as file:
wisdom = "The path to enlightenment begins with Python."
file.write(wisdom)
print("Ancient wisdom has been inscribed.")
## The file is automatically closed at the end of the with statement block.
스크립트를 실행합니다:
python write_file.py
다음 정보가 터미널에 표시되어야 합니다:
Ancient wisdom has been inscribed.
동시에 ancient_wisdom.txt라는 파일이 생성되며, 내용은 다음과 같습니다: The path to enlightenment begins with Python.