Extract data from multiple text files

Cameron Simpson cs at cskk.id.au
Tue May 15 22:47:30 EDT 2018


On 15May2018 18:18, MRAB <python at mrabarnett.plus.com> wrote:
>On 2018-05-15 13:12, mahesh d wrote:
>>import glob,os
[...]
>>files = glob.glob(path)

You've got a list of filenames here - they would work, _if_ you had passed in a 
glob pattern, instead of just the directory path. Try this again, joining 
"*.msg" or "*.txt" to the path.

>>for file in os.listdir(path):

As MRAB has mentioned, this just gets the file basenames. You need to join them 
to the directory path to get the proper pathname.

[...snop...]
>>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

Please _always_ include a text transcript of the errors, inline in the message 
(not as an attached file).

>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.

In particular, since it may not be obvious how to do this correctly:
import the os.path module and look at the os.path.join function.

And finally, _please_ post follow on questions to this list as _replies_ to the 
appropriate list message. By just posting new messages from scratch your new 
message is disconnected from all the earlier ones. Anyone trying to follow your 
discussion will lose all the other context.

Cheers,
Cameron Simpson <cs at cskk.id.au> (formerly cs at zip.com.au)



More information about the Python-list mailing list