Problem parsing files when extracting filenames os.listdir() as
If I use filename = argv[n]
from a windows command line, the extract()
function below seems to work okay. If I instead use the list of files names from the list(dir)
, which uses the os.listdir()
built-in f开发者_运维知识库unction to extract the filenames from the working directory, then the extract()
function fails.
The input_file.read() recognises the filename as a valid value, but it seems to fail at date = list(date_match[0]
) with 'TypeError: coercing to Unicode: need string or buffer, tuple found'.
It seems that the os.listdir output list values are not normal strings, but something else instead. Any ideas?
import sys
import re
import os
def extract(filename):
input_file = open(filename, 'r')
data = input_file.read() #read file line by line
#make list of time and date
date_match = re.findall(r'(\d+:\d+)\t(\d+/\d+/\d+)', data) #find file date and time
date = list(date_match[0])
#extract date tuple from list above
return date
def list(dir):
directoryfiles = os.listdir(dir)
diroutput = []
for member in directoryfiles:
if member != sys.argv[0]:
diroutput.append(member)
return diroutput
def main():
inputfiles = list(sys.argv[1])
for filename in inputfiles:
date = extract(filename)
if __name__ == '__main__':
main()
You've redefined list
! When you try to do date = list(date_match[0])
, you're calling your list
function, not the builtin list
function. Rename the list function and it should work fine.
精彩评论