0% found this document useful (0 votes)
23 views6 pages

Understanding Python's shutil Module

The document discusses Python's standard library modules for common tasks like file and directory manipulation (os, shutil), path manipulation (os.path), running external commands (os, sh), environment variables (os.environ, os.getenv), pattern matching files (glob), system information (sys), and object serialization (pickle). It provides examples of using each module to list directories, rename files, get paths, run commands, access environment variables, find files, and dump objects to files. The document is intended as notes for an introduction to Python's standard library.

Uploaded by

Manu Mannu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views6 pages

Understanding Python's shutil Module

The document discusses Python's standard library modules for common tasks like file and directory manipulation (os, shutil), path manipulation (os.path), running external commands (os, sh), environment variables (os.environ, os.getenv), pattern matching files (glob), system information (sys), and object serialization (pickle). It provides examples of using each module to list directories, rename files, get paths, run commands, access environment variables, find files, and dump objects to files. The document is intended as notes for an introduction to Python's standard library.

Uploaded by

Manu Mannu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

11/16/2014

1.2.7. Standard Library Scipy lecture notes

1.2.7. Standard Library


Note: Reference document for this section:
The Python Standard Library documentation: [Link]
Python Essential Reference, David Beazley, Addison-Wesley Professional

[Link].

os

module: operating system functionality

A portable way of using operating system dependent functionality.

[Link].1. Directory and file manipulation


Current directory:
In [17]: [Link]()
Out[17]: '/Users/cburns/src/scipy2009/scipy_2009_tutorial/source'
List a directory:
In [31]: [Link]([Link])
Out[31]:
['.[Link]',
'.python_language.[Link]',
'.view_array.[Link]',
'_static',
'_templates',
'basic_types.rst',
'[Link]',
'control_flow.rst',
'[Link]',
...
Make a directory:
In [32]: [Link]('junkdir')
In [33]: 'junkdir' in [Link]([Link])
Out[33]: True
Rename the directory:
In [36]: [Link]('junkdir', 'foodir')
In [37]: 'junkdir' in [Link]([Link])
Out[37]: False
In [38]: 'foodir' in [Link]([Link])
Out[38]: True
[Link]

1/6

11/16/2014

1.2.7. Standard Library Scipy lecture notes

In [41]: [Link]('foodir')
In [42]: 'foodir' in [Link]([Link])
Out[42]: False
Delete a file:
In [44]: fp = open('[Link]', 'w')
In [45]: [Link]()
In [46]: '[Link]' in [Link]([Link])
Out[46]: True
In [47]: [Link]('[Link]')
In [48]: '[Link]' in [Link]([Link])
Out[48]: False

[Link].2.
[Link]

[Link] :

path manipulations

provides common operations on pathnames.

In [70]: fp = open('[Link]', 'w')


In [71]: [Link]()
In [72]: a = [Link]('[Link]')
In [73]: a
Out[73]: '/Users/cburns/src/scipy2009/scipy_2009_tutorial/source/[Link]'
In [74]: [Link](a)
Out[74]: ('/Users/cburns/src/scipy2009/scipy_2009_tutorial/source',
'[Link]')
In [78]: [Link](a)
Out[78]: '/Users/cburns/src/scipy2009/scipy_2009_tutorial/source'
In [79]: [Link](a)
Out[79]: '[Link]'
In [80]: [Link]([Link](a))
Out[80]: ('junk', '.txt')
In [84]: [Link]('[Link]')
Out[84]: True
In [86]: [Link]('[Link]')
Out[86]: True
In [87]: [Link]('[Link]')
[Link]

2/6

11/16/2014

1.2.7. Standard Library Scipy lecture notes

Out[87]: False
In [88]: [Link]('~/local')
Out[88]: '/Users/cburns/local'
In [92]: [Link]([Link]('~'), 'local', 'bin')
Out[92]: '/Users/cburns/local/bin'

[Link].3. Running an external command


In [8]: [Link]('ls')
basic_types.rst
[Link]
standard_library.rst
control_flow.rst [Link]
[Link]
first_steps.rst

[Link]

python_language.rst

[Link]
[Link]

[Link]
reusing_code.rst

Note: Alternative to [Link]


A noteworthy alternative to [Link] is the sh module. Which provides much more convenient ways to
obtain the output, error stream and exit code of the external command.
In [20]: import sh
In [20]: com = [Link]()
In [21]: print com
basic_types.rst
[Link]
standard_library.rst
control_flow.rst first_steps.rst
[Link]
[Link]
[Link]
[Link]

[Link]
python_language.rst
[Link]
reusing_code.rst

In [22]: print com.exit_code


0
In [23]: type(com)
Out[23]: [Link]

[Link].4. Walking a directory


[Link]

generates a list of filenames in a directory tree.

In [10]: for dirpath, dirnames, filenames in [Link]([Link]):


....:
for fp in filenames:
....:
print [Link](fp)
....:
....:
/Users/cburns/src/scipy2009/scipy_2009_tutorial/source/.[Link]
/Users/cburns/src/scipy2009/scipy_2009_tutorial/source/.view_array.[Link]
/Users/cburns/src/scipy2009/scipy_2009_tutorial/source/basic_types.rst
/Users/cburns/src/scipy2009/scipy_2009_tutorial/source/[Link]
/Users/cburns/src/scipy2009/scipy_2009_tutorial/source/control_flow.rst
[Link]

3/6

11/16/2014

1.2.7. Standard Library Scipy lecture notes

...

[Link].5. Environment variables:


In [9]: import os
In [11]: [Link]()
Out[11]:
['_',
'FSLDIR',
'TERM_PROGRAM_VERSION',
'FSLREMOTECALL',
'USER',
'HOME',
'PATH',
'PS1',
'SHELL',
'EDITOR',
'WORKON_HOME',
'PYTHONPATH',
...
In [12]: [Link]['PYTHONPATH']
Out[12]: '.:/Users/cburns/src/utils:/Users/cburns/src/nitools:
/Users/cburns/local/lib/python2.5/site-packages/:
/usr/local/lib/python2.5/site-packages/:
/Library/Frameworks/[Link]/Versions/2.5/lib/python2.5'
In [16]: [Link]('PYTHONPATH')
Out[16]: '.:/Users/cburns/src/utils:/Users/cburns/src/nitools:
/Users/cburns/local/lib/python2.5/site-packages/:
/usr/local/lib/python2.5/site-packages/:
/Library/Frameworks/[Link]/Versions/2.5/lib/python2.5'

[Link].
The

shutil

shutil :

high-level file operations

provides useful file operations:


[Link] :

Recursively delete a directory tree.


[Link] : Recursively move a file or directory to another location.
[Link] : Copy files or directories.

[Link].
The

glob

glob :

Pattern matching on files

module provides convenient file pattern matching.

Find all files ending in

.txt :

In [18]: import glob


[Link]

4/6

11/16/2014

1.2.7. Standard Library Scipy lecture notes

In [19]: [Link]('*.txt')
Out[19]: ['holy_grail.txt', '[Link]', '[Link]']

[Link].

sys

module: system-specific information

System-specific information related to the Python interpreter.


Which version of python are you running and where is it installed:
In [117]: [Link]
Out[117]: 'darwin'
In [118]: [Link]
Out[118]: '2.5.2 (r252:60911, Feb 22 2008, 07:57:53) \n
[GCC 4.0.1 (Apple Computer, Inc. build 5363)]'
In [119]: [Link]
Out[119]: '/Library/Frameworks/[Link]/Versions/2.5'
List of command line arguments passed to a Python script:
In [100]: [Link]
Out[100]: ['/Users/cburns/local/bin/ipython']
[Link]

is a list of strings that specifies the search path for modules. Initialized from PYTHONPATH:

In [121]: [Link]
Out[121]:
['',
'/Users/cburns/local/bin',
'/Users/cburns/local/lib/python2.5/site-packages/[Link]',
'/Users/cburns/local/lib/python2.5/site-packages/[Link]',
'/Users/cburns/local/lib/python2.5/site-packages/[Link]',
'/Users/cburns/local/lib/python2.5/site-packages/[Link]',
'/Users/cburns/local/lib/python2.5/site-packages/[Link]',
...

[Link].

pickle :

easy persistence

Useful to store arbitrary objects to a file. Not safe or fast!


In [1]: import pickle
In [2]: l = [1, None, 'Stan']
In [3]: [Link](l, file('[Link]', 'w'))
In [4]: [Link](file('[Link]'))
Out[4]: [1, None, 'Stan']
[Link]

5/6

11/16/2014

1.2.7. Standard Library Scipy lecture notes

Exercise
Write a program to search your

PYTHONPATH

for the module

[Link] .

The PYTHONPATH Search Solution

[Link]

6/6

You might also like