Open file type file in python

I have little mail filessaved without any extension:

mail files

how to open them?

+4
source share
3 answers
with open('1', 'r') as fp:
    content = fp.read()

Thus, the file will always be closed.

+4
source

To add an answer to @ math2001, you can 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)
+3
source

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)
+2
source

Source: https://habr.com/ru/post/1671217/


All Articles