<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:foaf="http://xmlns.com/foaf/0.1/" xmlns:og="http://ogp.me/ns#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:schema="http://schema.org/" xmlns:sioc="http://rdfs.org/sioc/ns#" xmlns:sioct="http://rdfs.org/sioc/types#" xmlns:skos="http://www.w3.org/2004/02/skos/core#" xmlns:xsd="http://www.w3.org/2001/XMLSchema#" version="2.0" xml:base="https://www.linuxjournal.com/tag/sysamin">
  <channel>
    <title>SysAmin</title>
    <link>https://www.linuxjournal.com/tag/sysamin</link>
    <description/>
    <language>en</language>
    
    <item>
  <title>Automate Sysadmin Tasks with Python's os.walk Function</title>
  <link>https://www.linuxjournal.com/content/automate-sysadmin-tasks-pythons-oswalk-function</link>
  <description>  &lt;div data-history-node-id="1340107" class="layout layout--onecol"&gt;
    &lt;div class="layout__region layout__region--content"&gt;
      
            &lt;div class="field field--name-field-node-image field--type-image field--label-hidden field--item"&gt;  &lt;img src="https://www.linuxjournal.com/sites/default/files/nodeimage/story/bigstock--217339216_1.jpg" width="600" height="600" alt="Python logo" typeof="foaf:Image" class="img-responsive" /&gt;&lt;/div&gt;
      
            &lt;div class="field field--name-node-author field--type-ds field--label-hidden field--item"&gt;by &lt;a title="View user profile." href="https://www.linuxjournal.com/users/reuven-m-lerner" lang="" about="https://www.linuxjournal.com/users/reuven-m-lerner" typeof="schema:Person" property="schema:name" datatype="" xml:lang=""&gt;Reuven M. Lerner&lt;/a&gt;&lt;/div&gt;
      
            &lt;div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"&gt;&lt;p&gt;&lt;em&gt;Using Python's os.walk function to walk through a tree of files and
directories.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;
I'm a web guy; I put together my first site in early 1993. And
so, when I started to do Python training, I assumed that most of my
students also were going to be web developers or aspiring web
developers.  Nothing could be further from the truth. Although some of my
students certainly are interested in web applications, the majority of them
are software engineers, testers, data scientists and system
administrators.
&lt;/p&gt;

&lt;p&gt;
This last group, the system administrators, usually comes into my
course with the same story. The company they work for has been writing Bash
scripts for several years, but they want to move to a higher-level
language with greater expressiveness and a large number of third-party
add-ons. (No offense to Bash users is intended; you can do amazing
things with Bash, but I hope you'll agree that the scripts can become
unwieldy and hard to maintain.)
&lt;/p&gt;

&lt;p&gt;
It turns out that with a few simple tools and ideas, these system
administrators can use Python to do more with less code, as well as create
reports and maintain servers.  So in this article, I describe
one particularly useful tool that's often overlooked: os.walk, a
function that lets you walk through a tree of files and
directories.
&lt;/p&gt;

&lt;h3&gt;
os.walk Basics&lt;/h3&gt;

&lt;p&gt;
Linux users are used to the &lt;code&gt;ls&lt;/code&gt; command to get a list of files in a
directory. Python comes with two different functions that can return
the list of files. One is &lt;code&gt;os.listdir&lt;/code&gt;, which means the "listdir"
function in the "os" package. If you want, you can pass the name of a
directory to &lt;code&gt;os.listdir&lt;/code&gt;. If you don't do that, you'll get the names
of files in the current directory.  So, you can say:

&lt;/p&gt;&lt;pre&gt;
&lt;code&gt;
In [10]: import os
&lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;
When I do that on my computer, in the current directory, I get the following:

&lt;/p&gt;&lt;pre&gt;
&lt;code&gt;
In [11]: os.listdir('.')
Out[11]:
['.git',
 '.gitignore',
 '.ipynb_checkpoints',
 '.mypy_cache',
 'Archive',
 'Files']
&lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;
As you can see, &lt;code&gt;os.listdir&lt;/code&gt; returns a list of strings, with each
string being a filename. Of course, in UNIX-type systems, directories
are files too—so along with files, you'll also see subdirectories
without any obvious indication of which is which.
&lt;/p&gt;

&lt;p&gt;
I gave up on &lt;code&gt;os.listdir&lt;/code&gt; long ago, in favor of
&lt;code&gt;glob.glob&lt;/code&gt;, which means
the "glob" function in the "glob" module. Command-line users are used
to using "globbing", although they often don't know its name. Globbing
means using the * and ? characters, among others, for more flexible
matching of filenames.  Although &lt;code&gt;os.listdir&lt;/code&gt; can return the list of
files in a directory, it cannot filter them. You can though with
&lt;code&gt;glob.glob&lt;/code&gt;:

&lt;/p&gt;&lt;/div&gt;
      
            &lt;div class="field field--name-node-link field--type-ds field--label-hidden field--item"&gt;  &lt;a href="https://www.linuxjournal.com/content/automate-sysadmin-tasks-pythons-oswalk-function" hreflang="en"&gt;Go to Full Article&lt;/a&gt;
&lt;/div&gt;
      
    &lt;/div&gt;
  &lt;/div&gt;

</description>
  <pubDate>Tue, 13 Nov 2018 12:30:00 +0000</pubDate>
    <dc:creator>Reuven M. Lerner</dc:creator>
    <guid isPermaLink="false">1340107 at https://www.linuxjournal.com</guid>
    </item>

  </channel>
</rss>
