목록2024/04/24 (18)
SeouliteLab
list1 = [1, 2, 3, 4] list2 = ['a', 'b', 'c', 'd'] # 두 리스트를 병렬로 반복하여 출력하기 for item1, item2 in zip(list1, list2): print(item1, item2) 설명: 이 프로그램은 두 개의 리스트를 병렬로 반복하여 각 리스트의 원소를 함께 처리하는 기능을 수행합니다. zip() 함수를 사용하여 두 리스트를 병렬로 묶습니다. zip() 함수는 동일한 위치에 있는 원소들을 튜플 형태로 묶어줍니다. for 반복문에서 zip(list1, list2)를 사용하여 각 리스트의 원소들을 병렬로 가져와서 item1과 item2 변수에 할당합니다. print() 함수를 사용하여 item1..
import os # 현재 작업 디렉토리의 전체 경로 가져오기 current_directory = os.getcwd() print("현재 작업 디렉토리의 전체 경로:", current_directory) 설명: 이 프로그램은 현재 작업 디렉토리의 전체 경로를 가져오는 기능을 수행합니다. os 모듈을 사용하여 현재 작업 디렉토리의 경로를 가져오기 위해 os.getcwd() 함수를 호출합니다. 이 함수는 현재 파이썬 스크립트가 실행되는 디렉토리의 경로를 반환합니다. 반환된 경로는 현재 작업 디렉토리의 전체 경로이며, 이를 출력하여 사용자에게 보여줍니다. 해시태그: Python, 파이썬, 디렉토리, 작업디렉토리, 경로 import os # 현재 작업 디렉토리의 전체 경로 가져오기 current_director..
import os import datetime def get_file_dates(file_path): # 파일 생성일자 가져오기 creation_time = os.path.getctime(file_path) creation_date = datetime.datetime.fromtimestamp(creation_time) # 파일 수정일자 가져오기 modification_time = os.path.getmtime(file_path) modification_date = datetime.datetime.fromtimestamp(modification_time) return creation_date, modification_date # 테스트용 파일 경로 file_path = 'example.txt'..
import os def find_txt_files(directory): txt_files = [] for root, dirs, files in os.walk(directory): for file in files: if file.endswith(".txt"): txt_files.append(os.path.join(root, file)) return txt_files # 테스트용 디렉토리 경로 directory_path = '/path/to/directory' # 디렉토리 내부의 .txt 파일 찾기 txt_files = find_txt_files(directory_path) print(f"디렉토리 '{directory_path}' 내부의 .txt 파일 목록:") for txt_..
def count_lines(file_path): with open(file_path, 'r') as file: line_count = 0 for line in file: line_count += 1 return line_count # 테스트용 파일 경로 file_path = 'example.txt' # 파일의 줄 수 세기 num_lines = count_lines(file_path) print(f"파일 '{file_path}'의 줄 수:", num_lines) 설명: 이 프로그램은 주어진 파일의 줄 수를 세는 기능을 수행합니다. count_lines 함수는 파일 경로를 입력받아 해당 파일을 열고, 각 줄을 반복하여 세는 작업을 수행합니다. with open(f..
def calculate(x, y): sum_result = x + y difference_result = x - y product_result = x * y return sum_result, difference_result, product_result # 함수 호출 및 결과 출력 sum_result, difference_result, product_result = calculate(10, 5) print("덧셈 결과:", sum_result) print("뺄셈 결과:", difference_result) print("곱셈 결과:", product_result) 설명: 파이썬에서는 함수가 여러 개의 값을 반환할 수 있습니다. 이를 위해서는 return 문을 사용하여 여러 개의 값을 쉼표로 구분하여 반환하..
from enum import Enum, auto class Color(Enum): RED = auto() GREEN = auto() BLUE = auto() print(Color.RED) print(Color.GREEN) print(Color.BLUE) 설명: enum 모듈을 사용하여 열거형을 정의합니다. 열거형은 서로 연관된 상수들의 집합을 나타내며, 각 상수에는 이름과 값이 지정됩니다. Color 열거형은 RED, GREEN, BLUE 세 가지 색을 정의합니다. auto() 함수를 사용하여 자동으로 값을 할당할 수 있습니다. 이를 통해 각 상수의 값은 서로 다른 숫자로 자동으로 지정됩니다. Color.RED, Color.GREEN, Color.BLUE 등으로 각 상수에 접근할 수 있습니다.
파일 경로로부터 파일 이름을 추출하는 파이썬 프로그램을 작성해보겠습니다. 이 프로그램은 주어진 파일 경로에서 파일 이름만을 추출하여 반환하는 기능을 수행합니다. def get_file_name(file_path): # 파일 경로를 '/'를 기준으로 분할하여 리스트로 저장 path_parts = file_path.split('/') # 가장 마지막 요소가 파일 이름이므로 해당 요소를 반환 return path_parts[-1] # 테스트용 파일 경로 file_path = '/home/user/documents/example.txt' # 파일 이름 추출 및 출력 file_name = get_file_name(file_path) print("파일 경로:", file_..