The Wayback Machine - https://web.archive.org/web/20240928005850/https://www.geeksforgeeks.org/python-os-path-getctime-method/
Open In App

Python | os.path.getctime() method

Last Updated : 22 May, 2019
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name manipulation.

os.path.getctime() method in Python is used to get system’s ctime of the specified path. Here ctime refers to the last metadata change for specified path in UNIX while in Windows, it refers to path creation time.
This method returns a floating point value which represents the number of seconds since the epoch. This method raise OSError if the file does not exist or is somehow inaccessible.

Note: The epoch represents the point where the time starts. It is platform dependent. For Unix, the epoch is January 1, 1970, 00:00:00 (UTC).

Syntax: os.path.getctime(path)

Parameter:
path: A path-like object representing a file system path. A path-like object is either a string or bytes object representing a path.

Return Type: This method returns a floating-point value of class ‘float’ that represents the ctime (in seconds) for the specified path.

Code #1: Use of os.path.getctime() method




# Python program to explain os.path.getctime() method 
    
# importing os and time module 
import os
import time
  
# Path
path = '/home/User/Documents/file.txt'
  
# Get the ctime of last
# for the specified path
c_time = os.path.getctime(path)
print("ctime since the epoch:", c_time)
  
# convert the ctime in
# seconds since epoch
# to local time
local_time = time.ctime(c_time)
print("ctime (Local time):", local_time)


Output:

ctime since the epoch: 1558447897.3122742
ctime (Local time): Tue May 21 19:41:37 2019

Code #2: Handling error while using os.path.getctime() method




# Python program to explain os.path.getctime() method 
    
# importing os, time and sys module 
import os
import sys
import time
  
# Path
path = '/home/User/Documents/file2.txt'
  
# Get the ctime 
# for the specified path
try:
    c_time = os.path.getctime(path)
    print("ctime since the epoch:", c_time)
  
except OSError:
    print("Path '%s' does not exists or is inaccessible" %path)
    sys.exit()
  
# convert ctime in
# seconds since epoch
# to local time
local_time = time.ctime(c_time)
print("ctime(Local time):", local_time)
  
  
# above code will print
# path does not exists or is inaccessible'
# if the specified path does not
# exists or is inaccessible
  


Output:

Path '/home/User/Documents/file2.txt' does not exists or is inaccessible

Reference: https://docs.python.org/3/library/os.path.html



Previous Article
Next Article

Similar Reads

Python | os.path.splitdrive() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is a submodule of OS module in Python used for common pathname manipulation. os.path.splitdrive() method in Python i
3 min read
Python | os.path.getatime() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name manipulation. os.path.getatime() method in Python is
2 min read
Python | os.path.getmtime() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name manipulation.os.path.getmtime() method in Python is u
2 min read
Python | os.path.isfile() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name manipulation. os.path.isfile() method in Python is us
1 min read
Python | os.path.commonprefix() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is submodule of OS module in Python used for common path name manipulation. os.path.commonprefix() method in Python
2 min read
Python | os.path.commonpath() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is submodule of OS module in Python used for common path name manipulation. os.path.commonpath() method in Python is
2 min read
Python | os.path.splitext() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is submodule of OS module in Python used for common pathname manipulation. os.path.splitext() method in Python is us
2 min read
Python | os.path.expandvars() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path the module is a submodule of OS module in Python used for common pathname manipulation. os.path.expandvars() method in Pyth
3 min read
Python | os.path.islink() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name manipulation. os.path.islink() method in Python is us
2 min read
Python | os.path.lexists() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in python used for common path name manipulation. os.path.lexists() method in Python is u
2 min read
Python | os.path.samefile() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name manipulation. os.path.samefile() method in Python is
2 min read
Python | os.path.sameopenfile() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name manipulation.os.path.sameopenfile() method in Python
3 min read
Python | os.path.samestat() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name manipulation. os.path.samestat() method in Python is
2 min read
Python | os.path.realpath() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name manipulation. os.path.realpath() method in Python is
2 min read
Python | os.path.relpath() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name manipulation. os.path.relpath() method in Python is u
3 min read
Python | os.path.expanduser() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common pathname manipulation.os.path.expanduser() method in Python is
4 min read
Python PIL | ImagePath.Path.tolist() method
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The ImagePath module is used to store and manipulate 2-dimensional vector data. Path objects can be passed to the methods on the ImageDraw module. ImagePath.Path.tolist() Converts the path to a Python list [(x, y), …]. Syntax: PIL.ImagePath.Path
1 min read
Python PIL | ImagePath.Path.map() method
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The ImagePath module is used to store and manipulate 2-dimensional vector data. Path objects can be passed to the methods on the ImageDraw module. ImagePath.Path.map() maps the path through the function. Syntax: ImagePath.Path.map(function) Para
1 min read
Python PIL | ImagePath.Path.getbbox() method
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The ImagePath module is used to store and manipulate 2-dimensional vector data. Path objects can be passed to the methods on the ImageDraw module. ImagePath.Path.getbbox() Gets the bounding box of the path. Syntax: ImagePath.Path.getbbox() Param
1 min read
Python | os.path.normpath() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name manipulation. os.path.normpath() method in Python is
2 min read
Python | os.path.normcase() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name manipulation.os.path.normcase() method in Python is u
2 min read
Python | os.path.dirname() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name manipulation. os.path.dirname() method in Python is u
2 min read
Python | os.path.isabs() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name manipulation. os.path.isabs() method in Python is use
2 min read
Python | os.path.split() method
os.path.split() method in Python is used to Split the path name into a pair, and tail. Here, the tail is the last path name component and the head is everything leading up to that. For example, consider the following path names: path name = '/home/User/Desktop/file.txt' In the above example, the ‘file.txt’ component of the path name is tail, and ‘/
3 min read
Python | os.path.size() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system-dependentthe functionality. os.path module is a submodule of the OS module in Python used for common path name manipulation. In this article, we will discus
3 min read
Python | os.path.exists() method
os.path.exists() method in Python is used to check whether the specified path exists or not. This method can be also used to check whether the given path refers to an open file descriptor or not. os.path.exists() Syntax in PythonSyntax: os.path.exists(path) Parameter: path: A path-like object representing a file system path. A path-like object is e
2 min read
Python | os.path.ismount() method
os.path module is sub module of Python OS Module in Python used for common path name manipulation. os.path.ismount() method in Python is used to check whether the given path is a mount point or not. A mount point is a point in a file system where a different file system has been mounted. os.path.ismount() Syntax in PythonSyntax: os.path.ismount(pat
2 min read
Python | os.path.isdir() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system-dependent functionality. os.path module is a sub-module of the OS module in Python used for common path name manipulation. os.path.isdir() method in Python
2 min read
Python | os.path.join() method
Os Path Module is a sub-module of the OS module in Python used for common pathname manipulation. In this article, we will learn about os.path.join() and handling file paths safely in Python OS. Python os.path.join() Method SyntaxSyntax: os.path.join(path, *paths) Parameter: path: A path-like object representing a file system path. *path: A path-lik
4 min read
Python | os.path.basename() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name manipulation.os.path.basename() method in Python is u
4 min read
Article Tags :
Practice Tags :