Prashant Vikram Singh
Prashant Vikram Singh

Reputation: 101

Open a file of Type file in python

I have few mail files stored without any extension:

mail files

how to open them??

Upvotes: 4

Views: 5397

Answers (3)

systemjack
systemjack

Reputation: 2985

Yet another way:

import os
import glob
files = filter(os.path.isfile, glob.glob("./[0-9]*"))
for name in files:
    with open(name) as fh:
        contents = fh.read()
        # do something with contents (parse email format)

Upvotes: 2

Pike D.
Pike D.

Reputation: 691

To add on to @math2001's answer, you could do something like this:

numOfFiles = #int
data = []

for files in range(1, numOfFiles+1):
    with open(str(files), 'r') as f:
        // do whatever data processing you need to do
        fileData = f.read()
        data.append(fileData)

Upvotes: 3

Mathieu Paturel
Mathieu Paturel

Reputation: 4500

with open('1', 'r') as fp:
    content = fp.read()

This way, the file will always be closed.

Upvotes: 4

Related Questions