Extract data from multiple text files

MRAB python at mrabarnett.plus.com
Tue May 15 13:18:49 EDT 2018


On 2018-05-15 13:12, mahesh d wrote:
> import glob,os
> 
> import errno
> 
> path = 'C:/Users/A-7993\Desktop/task11/sample emails/'
> 
> files = glob.glob(path)
> 
> '''for name in files:
> 
>      print(str(name))
> 
>      if name.endswith(".txt"):
> 
>             print(name)'''
> 
> for file in os.listdir(path):
> 
>      print(file)
> 
>      if file.endswith(".txt"):
> 
>          print(os.path.join(path, file))
> 
>          print(file)
> 
>          try:
> 
>              with open(file) as f:
> 
>                  msg = f.read()
> 
>                  print(msg)
> 
>          except IOError as exc:
> 
>              if exc.errno != errno.EISDIR:
> 
>                  raise
> 
> 
> In the above program . Getting lot of errors . My intention is read the
> list of the text files in a folder . Print them
> 
> 
>   How can resolve those error
> 
You don't say what the errors are, but I'm guessing that it's 
complaining that it can't find the file with trying to open it.

'open' needs the _full path_ to the file, but 'os.listdir' returns only 
the _name_ of the files and directories, not the full path.



More information about the Python-list mailing list