GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • w3resource
  • Front End
  • HTML
  • CSS
  • JavaScript
  • HTML5
  • Schema.org
  • php.js
  • Twitter Bootstrap
  • Responsive Web Design tutorial
  • Zurb Foundation 3 tutorials
  • Pure CSS
  • HTML5 Canvas
  • JavaScript Course

This view

  • Original page
  • New address

GeezerVison is showing www.w3resource.com — Python: Retrieve file properties - w3resource

Python: Retrieve file properties

Last update on May 17 2025 05:43:18 (UTC/GMT +8 hours)


File Properties Retriever

Write a Python program to retrieve file properties.

Sample Solution:

Python Code:

# Import the 'os.path' and 'time' modules for working with file paths and time-related functions.
import os.path
import time
# Print the name of the current file using '__file__' attribute.
print('File         :', __file__)

# Print the access time of the current file using 'os.path.getatime()' and 'time.ctime()'.
print('Access time  :', time.ctime(os.path.getatime(__file__)))

# Print the modified time of the current file using 'os.path.getmtime()' and 'time.ctime()'.
print('Modified time:', time.ctime(os.path.getmtime(__file__)))

# Print the change time of the current file using 'os.path.getctime()' and 'time.ctime()'.
print('Change time  :', time.ctime(os.path.getctime(__file__)))

# Print the size of the current file using 'os.path.getsize()'.
print('Size         :', os.path.getsize(__file__))

Sample Output:

File         : 8dbacd90-266d-11e7-a9c1-cf681af3cdf1.py             
Access time  : Fri Apr 21 14:06:03 2017                            
Modified time: Fri Apr 21 14:06:02 2017                            
Change time  : Fri Apr 21 14:06:02 2017                            
Size         : 304   

Flowchart:

Flowchart: Retrieve file properties.


For more Practice: Solve these Related Problems:

  • Write a Python program to get the creation and modification time of a file.
  • Write a Python program to check the size of multiple files in a directory.
  • Write a Python program to retrieve the owner of a file.
  • Write a Python program to check if a file is read-only.

Go to:

  • Python Basic Exercises Home ↩
  • Python Exercises Home ↩

Previous: Write a Python program to divide a path on the extension separator.
Next: Write a Python program to find path refers to a file or directory when you encounter a path name.

Python Code Editor:

 

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.


  • Daily Coding Challenges & Projects
  • Weekly Trends and Language Statistics



You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.