[Tutor] converting all files in a directory with subprocess

Tim Golden mail at timgolden.me.uk
Thu May 8 22:13:34 CEST 2008


Tim Michelsen wrote:
> Hello,
> I am working on a automatic documentation program to convert my txt2tags 
> based documentations sources into HTMl files. I want to use txt2tags via 
> command line.
> 
> Here's my code:
> 
> #####
> 
> #!/usr/bin/env python
> # -*- coding: utf-8 -*-
> import os
> import subprocess
> import fnmatch
> 
> documentation_directory = './doc/'
> 
> for file in os.listdir(documentation_directory):
>     if fnmatch.fnmatch(file, '*.t2t'):
>         print file
>         subprocess.call('txt2tags', '-t html', '--toc')
> 
> ##### END ####
> 
> When I run the script it exits with the following error message:
> 
> #### START OUTPUT ####
> python ./make_documentation.py
> index.t2t
> Traceback (most recent call last):
>   File "./make_documentation.py", line 12, in <module>
>     subprocess.call('txt2tags', '-t html', '--toc')
>   File "/usr/lib/python2.5/subprocess.py", line 444, in call
>     return Popen(*popenargs, **kwargs).wait()
>   File "/usr/lib/python2.5/subprocess.py", line 545, in __init__
>     raise TypeError("bufsize must be an integer")
> TypeError: bufsize must be an integer
> #### END OUTPUT ####

Whenever you're running subprocess.call and you see this
error, say to yourself: "Have I passed the command line
as one parameter in a list; or have I passed it as a series
of parameters?" Because I can't remember the number of times
I've made that mistake myself. Basically, subprocess.call expects
either a string or a list of strings as the first param, and
then the second one is the bufsize, an integer. And then there
are others (check the params to subprocess.Popen). You're passing
a string -- your second param -- as the second parameter, and it's
not an integer. So you get a TypeError.

Simple, no?

TJG


More information about the Tutor mailing list