Using Wildcards, Copying, Deleting and Moving Files

We are going to see how os, glob and shutil modules in python to manipulate whole files.

Module shutil provides a set of higher level file manipulation commands. These includes the ability to copy individual files, copy whole directory trees, delete directory trees and move files or whole directory trees.One anomaly is the ability to delete a single file or group of files. That is actually found in the os module in the form of the os.remove() function for files os.rmdir() for empty directories, although shutil.rmtree() is more powerful and usually what you want.

Another useful module is glob. This module provides filename wildcard handling. You are probably familiar with "?" and "*" wildcards used to specify groups of files in the OS commands. For example "*.exe" specifies all the files ending in .exe. glob.glob() does the same thing in your code by returning a list of the matching filenames for a given pattern.

TRY IT OUT

  1. Change into the root directory if you already created or create a new directory called "root".
  2. Put empty text files in root directory you have created .
  3. Start the Python interpreter and type in the following code
>>> import os, glob, shutil as sh
>>> os.listdir('.')
['fileA.txt', 'fileB.txt', 'ls.txt', 'subdirectory']
>>> glob.glob('*') # everything in the folder
['fileA.txt', 'fileB.txt', 'ls.txt', 'subdirectory'] 
>>> glob.glob('*.*') . # any file name / all extensions 
['fileA.txt', 'fileB.txt', 'ls.txt']
>>> glob.glob("*.txt")
['fileA.txt', 'fileB.txt', 'ls.txt']
>>> glob.glob('file?.txt') # file start as prefix
['fileA.txt', 'fileB.txt']
>>> glob.glob("*.??") # all files extension 2 letters
[]
>>> glob.glob("*.???")
['fileA.txt', 'fileB.txt', 'ls.txt']

shutil usage example

>>> sh.copy('fileA.txt',"fileX.txt") # this create a file

'fileX.txt'

>>> sh.copy('fileB.txt','subdirectory/fileY.txt')

'subdirectory/fileY.txt'

>>> sh.copy('fileB.txt','subdirector/fileY.txt')

>>> sh.copytree('subdirectory','test3')
'test3'

>>> os.listdir('.')

['fileA.txt', 'fileB.txt', 'fileX.txt', 'ls.txt', 'subdirectory', 'test3']

>>> os.listdir('test3')

['fileY.txt']

>>> sh.rmtree('test3')

>>> os.listdir('.')

['fileA.txt', 'fileB.txt', 'fileX.txt', 'ls.txt', 'subdirectory']

>>> sh.move('subdirectory/fileY.txt','test5')
'test5'
>>> os.listdir('.')

['fileA.txt', 'fileB.txt', 'fileX.txt', 'ls.txt', 
'subdirectory', 'test5']

HOW IT WORK

In the first set of commands you compared the effect of os.listdir() with various patterns in the glob.glob() function. The first pattern ("*") replicated what os.listdir() did by listing the full contents of the folder. The second pattern ("*.*") listed all files with an extension. Next you used the "*text" pattern to find all the text files followed by 'file?.text' to find any "text" file whose name starts with file followed by a single character. Finally you used a combination of wildcards to find any file whose name ends in two characters ('*.??).

The Second set of commands looked at the shutil file manipulation commands. You started by using shutil.copy() to copy single files: fileA.txt to new file in the same folder called fileX.txt and fileB.txt to the subdirectory with a new name fileY.txt. Directory level shutil commands shutil.copytree() function that copied the subdirectory and its contents to a new folder , test3.
Finally removing file can be done with os.remove() this only work with files only. if you need to remove directory you need to use shutil.rmtree() or you can use os.rmdir() function to the same end.

Comments

Popular Posts