09 - Automation Through Scripting
09 - Automation Through Scripting
LESSON 9
https://docs.python.org/2/library/os.html#files-and-directories
"""
This program counts the number of .txt files
within the directory tree, starting at the
current directory.
"""
import os
count = 0
for dirname, dirs, files in os.walk('.'):
for filename in files:
if filename.endswith('.txt') :
count = count + 1
import os
from os.path import join
for (dirname, dirs, files) in os.walk('.'):
for filename in files:
if filename.endswith('.txt') :
thefile = os.path.join(dirname, filename)
print os.path.getsize(thefile), thefile
"""
This program how to process
command line arguments.
"""
import sys
"""
This program takes a file name as
a command line argument, opens
the specified file, and reads it.
"""
import sys
name = sys.argv[1]
handle = open(name, 'r')
text = handle.read()
print name, 'is', len(text), 'bytes'
"""
This program defines a command str,
opens that str as a pipe (which executes
the command), and reads all of the lines
returned by the command. The pipe is then
closed, and the status is printed.
"""
import os
status = file_pointer.close()
print status