[code.view]

[top] / python / PyMOTW / docs / articles / file_access.html


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>File Access &mdash; Python Module of the Week</title>
    <link rel="stylesheet" href="../_static/sphinxdoc.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '1.132',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="top" title="Python Module of the Week" href="../index.html" />
    <link rel="up" title="Features of the Standard Library" href="index.html" />
    <link rel="next" title="Text Processing Tools" href="text_processing.html" />
    <link rel="prev" title="In-Memory Data Structures" href="data_structures.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="text_processing.html" title="Text Processing Tools"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="data_structures.html" title="In-Memory Data Structures"
             accesskey="P">previous</a> |</li>
        <li><a href="../contents.html">PyMOTW</a> &raquo;</li>
          <li><a href="index.html" accesskey="U">Features of the Standard Library</a> &raquo;</li> 
      </ul>
    </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">File Access</a><ul>
<li><a class="reference internal" href="#filenames">Filenames</a></li>
<li><a class="reference internal" href="#meta-data">Meta-data</a></li>
<li><a class="reference internal" href="#reading-files">Reading Files</a></li>
<li><a class="reference internal" href="#temporary-files">Temporary Files</a></li>
<li><a class="reference internal" href="#files-and-directories">Files and Directories</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="data_structures.html"
                        title="previous chapter">In-Memory Data Structures</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="text_processing.html"
                        title="next chapter">Text Processing Tools</a></p>
  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="../_sources/articles/file_access.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="file-access">
<span id="article-file-access"></span><h1>File Access<a class="headerlink" href="#file-access" title="Permalink to this headline">¶</a></h1>
<p>Python&#8217;s standard library includes a large range of tools for working with files, filenames, and file contents.</p>
<div class="section" id="filenames">
<h2>Filenames<a class="headerlink" href="#filenames" title="Permalink to this headline">¶</a></h2>
<p>The first step in working with files is to get the name of the file so you can operate on it.  Python represents filenames as simple strings, but provides tools for building them from standard, platform-independent, components in <a class="reference internal" href="../ospath/index.html#module-os.path" title="os.path: Platform-independent manipulation of file names."><tt class="xref py py-mod docutils literal"><span class="pre">os.path</span></tt></a>.  List the contents of a directory with <tt class="docutils literal"><span class="pre">listdir()</span></tt> from <a class="reference internal" href="../os/index.html#module-os" title="os: Portable access to operating system specific features."><tt class="xref py py-mod docutils literal"><span class="pre">os</span></tt></a>, or use <a class="reference internal" href="../glob/index.html#module-glob" title="glob: Use Unix shell rules to fine filenames matching a pattern."><tt class="xref py py-mod docutils literal"><span class="pre">glob</span></tt></a> to build a list of filenames from a pattern.  Finer grained filtering of filenames is possible with <a class="reference internal" href="../fnmatch/index.html#module-fnmatch" title="fnmatch: Compare filenames against Unix-style glob patterns."><tt class="xref py py-mod docutils literal"><span class="pre">fnmatch</span></tt></a>.</p>
</div>
<div class="section" id="meta-data">
<h2>Meta-data<a class="headerlink" href="#meta-data" title="Permalink to this headline">¶</a></h2>
<p>Once you know the name of the file, you may want to check other characteristics such as permissions or the file size using <tt class="docutils literal"><span class="pre">os.stat()</span></tt> and the constants in <tt class="xref py py-mod docutils literal"><span class="pre">stat</span></tt>.</p>
</div>
<div class="section" id="reading-files">
<h2>Reading Files<a class="headerlink" href="#reading-files" title="Permalink to this headline">¶</a></h2>
<p>If you&#8217;re writing a filter application that processes text input line-by-line, <a class="reference internal" href="../fileinput/index.html#module-fileinput" title="fileinput: Process lines from input streams."><tt class="xref py py-mod docutils literal"><span class="pre">fileinput</span></tt></a> provides an easy framework to get started.  The fileinput API calls for you to iterate over the <tt class="docutils literal"><span class="pre">input()</span></tt> generator, processing each line as it is yielded.  The generator handles parsing command line arguments for file names, or falling back to reading directly from <tt class="docutils literal"><span class="pre">sys.stdin</span></tt>.  The result is a flexible tool your users can run directly on a file or as part of a pipeline.</p>
<p>If your app needs random access to files, <a class="reference internal" href="../linecache/index.html#module-linecache" title="linecache: Retrieve lines of text from files or imported python modules, holding a cache of the results to make reading many lines from the same file more efficient."><tt class="xref py py-mod docutils literal"><span class="pre">linecache</span></tt></a> makes it easy to read lines by their line number.  The contents of the file are maintained in a cache, so be careful of memory consumption.</p>
</div>
<div class="section" id="temporary-files">
<h2>Temporary Files<a class="headerlink" href="#temporary-files" title="Permalink to this headline">¶</a></h2>
<p>For cases where you need to create scratch files to hold data temporarily, or before moving it to a permanent location, <a class="reference internal" href="../tempfile/index.html#module-tempfile" title="tempfile: Create temporary filesystem resources."><tt class="xref py py-mod docutils literal"><span class="pre">tempfile</span></tt></a> will be very useful.  It provides classes to create temporary files and directories safely and securely.  Names are guaranteed not to collide, and include random components so they are not easily guessable.</p>
</div>
<div class="section" id="files-and-directories">
<h2>Files and Directories<a class="headerlink" href="#files-and-directories" title="Permalink to this headline">¶</a></h2>
<p>Frequently you need to work on a file as a whole, without worrying about what is in it.  The <a class="reference internal" href="../shutil/index.html#module-shutil" title="shutil: High-level file operations."><tt class="xref py py-mod docutils literal"><span class="pre">shutil</span></tt></a> module includes high-level file operations such as copying files and directories, setting permissions, etc.</p>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="text_processing.html" title="Text Processing Tools"
             >next</a> |</li>
        <li class="right" >
          <a href="data_structures.html" title="In-Memory Data Structures"
             >previous</a> |</li>
        <li><a href="../contents.html">PyMOTW</a> &raquo;</li>
          <li><a href="index.html" >Features of the Standard Library</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; Copyright Doug Hellmann.
      Last updated on Oct 24, 2010.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a>.

    <br/><a href="http://creativecommons.org/licenses/by-nc-sa/3.0/us/" rel="license"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-nc-sa/3.0/us/88x31.png"/></a>
    
    </div>
  </body>
</html>

[top] / python / PyMOTW / docs / articles / file_access.html

contact | logmethods.com