파이썬 파일 생성 및 수정 날짜/시간 가져오기
os 모듈 사용
os
모듈은 운영 체제와 상호 작용하는 데 사용되는 표준 라이브러리입니다. 다음 코드는 os.path.getctime()
및 os.path.getmtime()
함수를 사용하여 파일의 생성 및 수정 시간을 가져옵니다.
import os
filename = "example.txt"
# 파일 생성 시간 가져오기
creation_time = os.path.getctime(filename)
# 파일 수정 시간 가져오기
modification_time = os.path.getmtime(filename)
# 날짜/시간 형식 변환
from datetime import datetime
creation_time_str = datetime.fromtimestamp(creation_time).strftime("%Y-%m-%d %H:%M:%S")
modification_time_str = datetime.fromtimestamp(modification_time).strftime("%Y-%m-%d %H:%M:%S")
# 출력
print(f"파일 생성 시간: {creation_time_str}")
print(f"파일 수정 시간: {modification_time_str}")
stat 함수 사용
stat
함수는 파일 시스템 정보를 가져오는 데 사용됩니다. 다음 코드는 stat
함수를 사용하여 파일의 생성 및 수정 시간을 가져옵니다.
import stat
filename = "example.txt"
file_stats = stat.stat(filename)
# 파일 생성 시간 가져오기
creation_time = file_stats.st_ctime
# 파일 수정 시간 가져오기
modification_time = file_stats.st_mtime
# 날짜/시간 형식 변환
from datetime import datetime
creation_time_str = datetime.fromtimestamp(creation_time).strftime("%Y-%m-%d %H:%M:%S")
modification_time_str = datetime.fromtimestamp(modification_time).strftime("%Y-%m-%d %H:%M:%S")
# 출력
print(f"파일 생성 시간: {creation_time_str}")
print(f"파일 수정 시간: {modification_time_str}")
참고:
os.path.getctime()
및os.path.getmtime()
함수는 Unix 시스템에서만 작동합니다. Windows에서는stat
함수를 사용해야 합니다.
예시:
# example.txt 파일 생성
with open("example.txt", "w") as f:
f.write("Hello, world!")
# 파일 생성 및 수정 날짜/시간 출력
import os
filename = "example.txt"
# 파일 생성 시간 가져오기
creation_time = os.path.getctime(filename)
# 파일 수정 시간 가져오기
modification_time = os.path.getmtime(filename)
# 날짜/시간 형식 변환
from datetime import datetime
creation_time_str = datetime.fromtimestamp(creation_time).strftime("%Y-%m-%d %H:%M:%S")
modification_time_str = datetime.fromtimestamp(modification_time).strftime("%Y-%m-%d %H:%M:%S")
# 출력
print(f"파일 생성 시간: {creation_time_str}")
print(f"파일 수정 시간: {modification_time_str}")
출력:
파일 생성 시간: 2024-04-06 08:00:00
파일 수정 시간: 2024-04-06 08:00:00
예제 코드
# example.txt 파일 생성
with open("example.txt", "w") as f:
f.write("Hello, world!")
# 파일 생성 및 수정 날짜/시간 출력
import os
filename = "example.txt"
# 파일 생성 시간 가져오기
creation_time = os.path.getctime(filename)
# 파일 수정 시간 가져오기
modification_time = os.path.getmtime(filename)
# 날짜/시간 형식 변환
from datetime import datetime
creation_time_str = datetime.fromtimestamp(creation_time).strftime("%Y-%m-%d %H:%M:%S")
modification_time_str = datetime.fromtimestamp(modification_time).strftime("%Y-%m-%d %H:%M:%S")
# 출력
print(f"파일 생성 시간: {creation_time_str}")
print(f"파일 수정 시간: {modification_time_str}")
파일 생성 시간: 2024-04-06 08:00:00
파일 수정 시간: 2024-04-06 08:00:00
설명:
example.txt
파일을 "Hello, world!"라는 내용으로 생성합니다.os.path.getctime()
및os.path.getmtime()
함수를 사용하여 파일의 생성 및 수정 시간을 가져옵니다.datetime
모듈을 사용하여 날짜/시간을 원하는 형식으로 변환합니다.- 파일 생성 및 수정 시간을 출력합니다.
- 이 코드는 Unix 시스템에서만 작동합니다. Windows에서는
stat
함수를 사용해야 합니다. stat
함수는 파일의 생성 및 수정 시간 외에도 다른 많은 정보를 제공합니다. 자세한 내용은 stat 함수 문서를 참조하십시오.
추가 예시:
- 파일 크기 가져오기
- 파일 내용 읽기 및 쓰기
- 파일 삭제
대체 방법
import stat
filename = "example.txt"
file_stats = stat.stat(filename)
# 파일 생성 시간 가져오기
creation_time = file_stats.st_ctime
# 파일 수정 시간 가져오기
modification_time = file_stats.st_mtime
# 날짜/시간 형식 변환
from datetime import datetime
creation_time_str = datetime.fromtimestamp(creation_time).strftime("%Y-%m-%d %H:%M:%S")
modification_time_str = datetime.fromtimestamp(modification_time).strftime("%Y-%m-%d %H:%M:%S")
# 출력
print(f"파일 생성 시간: {creation_time_str}")
print(f"파일 수정 시간: {modification_time_str}")
pathlib 모듈 사용
pathlib
모듈은 Python 3.4 이상에서 사용할 수 있는 표준 라이브러리입니다. 다음 코드는 pathlib
모듈을 사용하여 파일의 생성 및 수정 시간을 가져옵니다.
from pathlib import Path
filename = "example.txt"
file_path = Path(filename)
# 파일 생성 시간 가져오기
creation_time = file_path.stat().st_ctime
# 파일 수정 시간 가져오기
modification_time = file_path.stat().st_mtime
# 날짜/시간 형식 변환
from datetime import datetime
creation_time_str = datetime.fromtimestamp(creation_time).strftime("%Y-%m-%d %H:%M:%S")
modification_time_str = datetime.fromtimestamp(modification_time).strftime("%Y-%m-%d %H:%M:%S")
# 출력
print(f"파일 생성 시간: {creation_time_str}")
print(f"파일 수정 시간: {modification_time_str}")
shutil 모듈 사용
shutil
모듈은 파일 및 폴더를 처리하는 데 사용되는 표준 라이브러리입니다. 다음 코드는 shutil
모듈을 사용하여 파일의 생성 및 수정 시간을 가져옵니다.
import shutil
filename = "example.txt"
# 파일 생성 시간 가져오기
creation_time = shutil.get_creation_time(filename)
# 파일 수정 시간 가져오기
modification_time = shutil.get_mtime(filename)
# 날짜/시간 형식 변환
from datetime import datetime
creation_time_str = datetime.fromtimestamp(creation_time).strftime("%Y-%m-%d %H:%M:%S")
modification_time_str = datetime.fromtimestamp(modification_time).strftime("%Y-%m-%d %H:%M:%S")
# 출력
print(f"파일 생성 시간: {creation_time_str}")
print(f"파일 수정 시간: {modification_time_str}")
subprocess 모듈 사용
subprocess
모듈은 하위 프로세스를 실행하는 데 사용되는 표준 라이브러리입니다. 다음 코드는 subprocess
모듈을 사용하여 stat
명령을 실행하고 파일의 생성 및 수정 시간을 가져옵니다.
import subprocess
filename = "example.txt"
# 파일 생성 시간 가져오기
creation_time = subprocess.check_output(["stat", "-c", "%y", filename]).decode().strip()
# 파일 수정 시간 가져오기
modification_time = subprocess.check_output(["stat", "-c", "%X", filename]).decode().strip()
# 날짜/시간 형식 변환
from datetime import datetime
creation_time_str = datetime.fromtimestamp(creation_time).strftime("%Y-%m-%d %H:%M:%S")
modification_time_str = datetime.fromtimestamp(modification_time).strftime("%Y-%m-%d %H:%M:%S")
# 출력
print(f"파일 생성 시간: {creation_time_str}")
print(f"파일 수정 시간: {modification_time_str}")
stat
명령 및dir
명령은 다른 형식의 출력을 제공합니다. 따라서 출력
python file