4

I have few mail files stored without any extension:

mail files

how to open them??

4
  • i don't get it, you know how to read a file but have no idea how to open it ;)? Commented Mar 2, 2017 at 6:29
  • Open like any other file: target_file = open(final_file_name, "rt") target_file.close() Commented Mar 2, 2017 at 6:29
  • @mic4ael i meant to say that if i can open the file then inside of that file i will be having the whole mail content. The mail content i know how to work with but i don't know how to open the file. Commented Mar 2, 2017 at 6:31
  • 1
    Please define "open"! Commented Mar 2, 2017 at 6:31

3 Answers 3

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

This way, the file will always be closed.

Sign up to request clarification or add additional context in comments.

Comments

3

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)

Comments

2

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.