파일 생성일이랑 수정일 알아내는 법

조회수 13336회

리눅스랑 윈도우에서 파일 생성일&수정일을 알아내고 싶은데 플랫폼 상관없이 코드 하나로 할 수 있는 방법 있나요?

1 답변

  • 좋아요

    0

    싫어요
    채택 취소하기

    수정일 - os.path.getmtime(path)

    주어진 경로가 마지막으로 수정된 날을 return합니다. 파일이 존재하지 않거나 접근할 수 없는 경우 os.error가 발생합니다.

    import os.path, time
    print "last modified: %s" % time.ctime(os.path.getmtime(file))
    print "created: %s" % time.ctime(os.path.getctime(file))
    

    수정일 - os.stat(path)

    파일에 대해 다음과 같은 정보를 담고있는 객체를 return합니다.

    • st_mode - protection bits
    • st_ino - inode number
    • st_dev - device
    • st_nlink - hard link의 갯수,
    • st_uid - owneruser id,
    • st_gid - ownergroup id,
    • st_size - 파일의 크기(byte단위),
    • st_atime - 마지막 접속 시간,
    • st_mtime - 마지막 수정일,
    • st_ctime - 마지막으로 메타데이터가 바뀐 시간(unix), 생성된 날짜(window)
    import os
    fileinfo = os.stat(path)
    print fileinfo.st_mtime
    

    생성일 - os.path.getctime

    import os, time
    (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) = os.stat(file)
    print "last modified: %s" % time.ctime(time)
    

    단, linux/unix시스템에서 ctime()은 생성일이 아니라 inode data가 바뀐 시간을 의미합니다.

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)