[파이썬] 파일 경로와 파일 메타데이터

파일 경로와 파일 메타데이터는 파일을 조작하고 처리하는 데 필요한 중요한 정보입니다. Python은 파일 경로를 다루는 데 매우 편리한 기능을 제공합니다. 또한, 파일의 메타데이터를 가져오고 수정하는 방법도 제공합니다.

파일 경로 다루기

파일 경로를 다루기 위해서는 os 모듈과 os.path 모듈을 사용할 수 있습니다. 다음은 파일 경로를 다루는 몇 가지 유용한 함수들입니다:

아래는 이러한 함수들을 사용한 예제입니다:

import os

path = os.path.join('path', 'to', 'file.txt')
print(f"Joined path: {path}")

abs_path = os.path.abspath(path)
print(f"Absolute path: {abs_path}")

dir_name = os.path.dirname(abs_path)
print(f"Directory name: {dir_name}")

file_name = os.path.basename(abs_path)
print(f"File name: {file_name}")

path_exists = os.path.exists(abs_path)
if path_exists:
    print("Path exists")
else:
    print("Path does not exist")

파일 메타데이터 가져오기

Python에서 파일의 메타데이터를 가져오기 위해서는 os 모듈과 os.path 모듈을 사용할 수 있습니다. 다음은 파일의 메타데이터를 가져오는 몇 가지 방법입니다:

아래는 이러한 함수들을 사용하여 파일의 메타데이터를 가져오는 예제입니다:

import os.path
import time

path = 'path/to/file.txt'

file_size = os.path.getsize(path)
print(f"File size: {file_size} bytes")

modification_time = os.path.getmtime(path)
formatted_time = time.ctime(modification_time)
print(f"Modification time: {formatted_time}")

creation_time = os.path.getctime(path)
formatted_time = time.ctime(creation_time)
print(f"Creation time: {formatted_time}")

파일 경로와 파일 메타데이터를 다루는 것은 Python 프로그래밍에서 매우 일반적인 작업입니다. 이러한 기능들을 효과적으로 사용하면 파일 시스템을 조작하고 파일의 정보를 얻는 데 도움이 됩니다.