Home arrow Python arrow Page 3 - File Management in Python

Directories - 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

 

Directories, like regular files, are easy to work with. Let's start by listing the contents of a directory: 

import os

for fileName in os.listdir ( '/' ):

   print fileName 

As you can see, this is extremely simple, and it can be done in three lines.

 

Creating a directory is also simple: 

import os

os.mkdir ( 'testDirectory' ) 

It is equally as easy to delete the directory we just created: 

import os

os.rmdir ( 'testDirectory ) 

We can also create multiple directories at a time: 

import os

os.makedirs ( 'I/will/show/you/how/deep/the/rabbit/hole/goes' ) 

Assuming we add nothing to the directories we just created, we can also delete them all at once: 

import os

os.removedirs ( 'I/will/show/you/how/deep/the/rabbit/hole/goes' ) 

Suppose we want to perform a specific action when a specific file type is reached. This can easily be done with the "fnmatch" module. Let's print the contents of all the ".txt" files we encounter and print the filename of any ".exe" files we encounter: 

import fnmatch

import os

for fileName in os.listdir ( '/' ):

   if fnmatch.fnmath ( fileName, '*.txt' ):

      print open ( fileName ).read()

   elif fnmatch.fnmatch ( fileName, '*.exe' ):

      print fileName 

The asterisk character can represent any amount of characters. If we want to match just one character, we can use the question mark: 

import fnmatch

import os

for fileName in os.listdir ( '/' ):

   if fnmatch.fnmatch ( fileName, '?.txt' ):

      print 'Text file.' 

It is also possible to create a regular expression using the "fnmatch" module, matching filenames with the "re" module:

 

import fnmatch

import os

import re

filePattern = fnmatch.translate ( '*.txt' )

for fileName in os.listdir ( '/' ):

   if re.match ( filePattern, fileName ):

      print 'Text file.'

 

If we're just looking for one type of filename, it is a lot easier to use the "glob" module. Its patterns are similar to those used in "fnmatch": 

import glob

for fileName in glob.glob ( '*.txt' ):

   print 'Text file.' 

It is also possible to use ranges of characters in the patterns, just as you would in regular expressions. Suppose you want to print the names of text files with one digit before the extension: 

import glob

for fileName in glob.glob ( '[0-9].txt' ):

   print fileName 

The "glob" module makes use of the "fnmatch" module.

 



 
 
>>> 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: