Python Module shutil: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
 
(6 intermediate revisions by the same user not shown)
Line 3: Line 3:


=Internal=
=Internal=
* [[Python Language Modularization#shutil|Python Language Modularization]]
* [[Python Language#shutil|Python Language | Standard Library]]
* [[File_Operations_in_Python#Recursively_Copy_a_Directory|File Operations in Python]]


=Overview=
=Overview=
Line 11: Line 12:
<syntaxhighlight lang='python'>
<syntaxhighlight lang='python'>
import shutil
import shutil
shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False, dirs_exist_ok=False)
shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False, dirs_exist_ok=False)
</syntaxhighlight>
</syntaxhighlight>
All required directories will be created if they don't exist.
==Arguments==
==Arguments==
====<tt>ignore</tt>====
====<tt>ignore</tt>====
Line 20: Line 21:
   callable(src, names) -> ignored_names
   callable(src, names) -> ignored_names
</font>
</font>
Since copytree() is called recursively, the callable will be called once for each directory that is copied. It returns a list of names relative to the <code>src</code> directory that should not be copied.
Since <code>copytree()</code> is called recursively, the callable will be called once for each directory that is copied. It returns a list of names relative to the <code>src</code> directory that should not be copied. shutil already contains a function <code>ignore_patterns</code>:
<syntaxhighlight lang='py'>
<syntaxhighlight lang='py'>
copytree(source, destination, ignore=ignore_patterns('*.pyc', 'tmp*'))
</syntaxhighlight>
</syntaxhighlight>
 
=Recursively Delete a Directory=
shutil already contains a function <code>ignore_patterns</code>:
<syntaxhighlight lang='python'>
<syntaxhighlight lang='py'>
import shutil
copytree(source, destination, ignore=ignore_patterns('*.pyc', 'tmp*'))
shutil.rmtree('/path/to/the/dir/')
</syntaxhighlight>
</syntaxhighlight>



Latest revision as of 01:32, 20 June 2022

External

Internal

Overview

The shutil module offers high-level operations on files and collections of files. In particular, functions are provided which support file copying and removal. For operations on individual files, see also the os module.

Recursively Copy a Directory

import shutil
shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False, dirs_exist_ok=False)

All required directories will be created if they don't exist.

Arguments

ignore

The optional ignore argument is a callable. If given, it is called with the src parameter, which is the directory being visited by copytree(), and names which is the list of src contents, as returned by os.listdir():

 callable(src, names) -> ignored_names

Since copytree() is called recursively, the callable will be called once for each directory that is copied. It returns a list of names relative to the src directory that should not be copied. shutil already contains a function ignore_patterns:

copytree(source, destination, ignore=ignore_patterns('*.pyc', 'tmp*'))

Recursively Delete a Directory

import shutil
shutil.rmtree('/path/to/the/dir/')

Organizatorium

from shutil import which
for i in m:
  print("{:20}{:7}".format(m[i], "OK" if which(i) else "missing"))