모든 청사진 파일 나열 및 내용 읽기
이 단계에서는 디렉토리 내의 모든 청사진 파일을 나열한 다음 특정 파일의 내용을 읽는 스크립트를 개발합니다.
이제 디렉토리에 여러 청사진 파일이 있다고 가정해 보겠습니다. /home/labex/project/ 디렉토리에 있는 모든 텍스트 파일 .txt를 나열하는 list_blueprints.py 스크립트를 엽니다.
## list_blueprints.py
import os
## Define the blueprints directory path
blueprints_dir = '/home/labex/project/'
## List all files in the directory
files = os.listdir(blueprints_dir)
## Filter out non-txt files and print the remaining files
print("Blueprint Files:")
for file in files:
if file.endswith('.txt'):
print(file)
## Assume 'oxygen_gardens.txt' is the file we want to read
file_to_read = 'oxygen_gardens.txt'
with open(os.path.join(blueprints_dir, file_to_read), 'r') as file:
print(f"\nContents of {file_to_read}:")
print(file.read())
프로젝트 디렉토리의 터미널에서 스크립트를 실행합니다.
python list_blueprints.py
출력은 모든 청사진 파일을 나열한 다음 oxygen_gardens.txt의 내용을 표시해야 합니다.
Blueprint Files:
oxygen_gardens.txt
Contents of oxygen_gardens.txt:
Celestia Oxygen Gardens Blueprint
Architect: [Your Name]
Last Updated: [Today's Date]
This space garden is designed to provide ample oxygen supply and recreational space to Celestia residents.