Working with File Properties in Python

In the following programs you will learn that how to check if file exist or not. Using getmtime() and getctime() functions of 'os.path' library we can view created and last modified date and time of file.

Program Printing Last Modified and Created Time of File

Program

import os.path, time
file = "C://MyTxtFile.txt"
print("last modified: %s" % time.ctime(os.path.getmtime(file)))
print("created: %s" % time.ctime(os.path.getctime(file)))
Output
last modified: Sun Oct 8 02:06:18 2023
created: Sun Oct 8 01:36:50 2023

Program Checking File Exists Or Not

Program

import os.path
print(os.path.exists("C://MyTxtFile.txt")) #True
Output
True

Program Printing Python Version and KeyWords

Program

import sys #U also Import Libraries like 'import sys,keyword'.
import keyword
print("Python version: ", sys.version_info)
print("Python keywords: ", keyword.kwlist)
Output
Python version: sys.version_info(major=3, minor=10, micro=5, releaselevel='final', serial=0)
Python keywords: ['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

Program Printing Program Directory
Program

import os
print(os.getcwd())
Output
C:\Users\Bilal Khan\PycharmProjects\MyProgram