Home arrow Python arrow Page 2 - File Management in Python

Getting Information on Existing Files - Python

File management is a basic function, and an important part of many applications. Python makes file management surprisingly easy, especially when compared to other languages. Peyton McCullough explains the basics.

TABLE OF CONTENTS:
  1. File Management in Python
  2. Getting Information on Existing Files
  3. Directories
  4. Pickling Data
  5. Creating In-memory Files
By: Peyton McCullough
Rating: starstarstarstarstar / 108
January 31, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

 

Using several of Python's modules, it is possible to obtain information on existig files. To get basic information, the "os" module can be used in conjunction with the "stat" module: 

import os

import stat

import time

fileStats = os.stat ( 'test.txt' )

fileInfo = {

   'Size' : fileStats [ stat.ST_SIZE ],

   'LastModified' : time.ctime ( fileStats [ stat.ST_MTIME ] ),

   'LastAccessed' : time.ctime ( fileStats [ stat.ST_ATIME ] ),

   'CreationTime' : time.ctime ( fileStats [ stat.ST_CTIME ] ),

   'Mode' : fileStats [ stat.ST_MODE ]

}

for infoField, infoValue in fileInfo:

   print infoField, ':' + infoValue

if stat.S_ISDIR ( fileStats [ stat.ST_MODE ] ):

   print 'Directory. '

else:

   print 'Non-directory.' 

The above example creates a dictionary containing some basic information about the file. It then displays the data and tells us if it's a directory or not. We can also check to see if the file is one of several other types: 

import os

import stat

fileStats = os.stat ( 'test.txt' )

fileMode = fileStats [ stat.ST_MODE ]

if stat.S_ISREG ( fileStats [ stat.ST_MODE ] ):

   print 'Regular file.'

elif stat.S_ISDIR ( fileSTats [ stat.ST_MODe ] ):

   print 'Directory.'

elif stat.S_ISLNK ( fileSTats [ stat.ST_MODe ] ):

   print 'Shortcut.'

elif stat.S_ISSOCK ( fileSTats [ stat.ST_MODe ] ):

   print 'Socket.'

elif stat.S_ISFIFO ( fileSTats [ stat.ST_MODe ] ):

   print 'Named pipe.'

elif stat.S_ISBLK ( fileSTats [ stat.ST_MODe ] ):

   print 'Block special device.'

elif stat.S_ISCHR ( fileSTats [ stat.ST_MODe ] ):

   print 'Character special device.' 

Additionally, we can use "os.path" to gather basic information: 

import os.path

fileStats = 'test.txt'

if os.path.isdir ( fileStats ):

   print 'Directory.'

elif os.path.isfile ( fileStats ):

   print 'File.'

elif os.path.islink ( fileStats ):

   print 'Shortcut.'

elif os.path.ismount ( fileStats ):

   print 'Mount point.' 



 
 
>>> More Python Articles          >>> More By Peyton McCullough
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: