#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    14
    Rep Power
    0

    Redirect stdout from subprocess Works, but Program Crashes


    As always, sorry if this is a basic question. I am running Python 2.6.6 on CentOS 6.2 (64 bit, 32GB of RAM). I want to do an 'ls' command from Python and put the result in a text file. My program accomplishes this but still crashes with an error:

    Code:
    databaseDirectory = './database'
    databaseFile = open('databaseList.txt', 'w')
    command = subprocess.Popen(['ls', databaseDirectory], stdout=subprocess.PIPE)
    fileList = command.communicate()
    
    for chunk in fileList:
    	for word in re.split('\n', chunk): # This line has an error
    		if len(word) > 0:
    			if '.tdf' in word:
    				databaseFile.write(word)
    				databaseFile.write('\n')
    The error is 'TypeError: expected string or buffer'

    Thanks for the help.

    -John
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,701
    Rep Power
    480
    Code:
    databaseDirectory = './database'
    databaseFile = open('databaseList.txt', 'w')
    command = subprocess.Popen(['ls', databaseDirectory], stdout=subprocess.PIPE)
    (stdout_string,none,) = command.communicate()   # communicate returns a tuple of 2 objects.  
    
    fileList = stdout_string
    for chunk in fileList:
    Please investigate os.listdir

    os.listdir(databaseDirectory)
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    14
    Rep Power
    0
    Thanks, this is a much better solution.

    -John

IMN logo majestic logo threadwatch logo seochat tools logo