简体   繁体   中英

read multiple txt files python

I have 6000 txt files to read in python. I am trying to read but all txt files are line by line.

Subject: key dates and impact of upcoming sap implementation over the next few weeks , project apollo and beyond will conduct its final sap implementation ) this implementation will impact approximately 12 , 000 new users plus all existing system users . sap brings a new dynamic to enron , enhancing the timely flow and sharing of specific project , human resources , procurement , and financial information across business units and across continents . this final implementation will retire multiple , disparate systems and replace them with a common , integrated system encompassing many processes including payroll , timekeeping ...

So python seperate it to rows when I read files one by one(I know thats ridiculos). In the end, 1 mail dividing multiple rows. I have tried read_csv all txt files but python give error that ValueError: stat: path too long for Windows . I don't know what should I do from now.

I tried this:

import glob
import errno
path =r'C:\Users\frknk\OneDrive\Masaüstü\enron6\emails\*.txt'
files = glob.glob(path)
for name in files:
    try:
        with open(name) as f:
            for line in f:
                print(line.split())
    except IOError as exc:
        if exc.errno != errno.EISDIR:
            raise

['Subject:', 'key', 'dates', 'and', 'impact', 'of', 'upcoming', 'sap', 'implementation']
['over', 'the', 'next', 'few', 'weeks', ',', 'project', 'apollo', 'and', 'beyond', 'will', 'conduct', 'its', 'final', 'sap']

I need this email by email but it seperated line by line. So what I want is each row represented by one email.

You can read the whole text file into a variable and later manipulate however you want. Just replace for line in f with data=f.read() .So, below I read each txt file into data variable and later I split to get words separated by " ". Hope this helps.

for name in files:
    try:
        with open(name) as f:
            data = f.read().replace("\n","") 
        print(data.split())
    except IOError as exc:
        if exc.errno != errno.EISDIR:
            raise

Output would look like this:

['Subject:', 'key', 'dates', 'and', 'impact', 'of', 'upcoming', 'sap', 'implementationover', 'the', 'next', 'few', 'weeks', ',', 'project', 'apollo', 'and', 'beyond', 'will', 'conduct', 'its', 'final', 'sapimplementation', ')', 'this', 'implementation', 'will', 'impact', 'approximately', '12', ',', '000', 'newusers', 'plus', 'all', 'existing', 'system', 'users', '.', 'sap', 'brings', 'a', 'new', 'dynamic', 'to', 'enron', ',enhancing', 'the', 'timely', 'flow', 'and', 'sharing', 'of', 'specific', 'project', ',', 'human', 'resources', ',procurement', ',', 'and', 'financial', 'information', 'across', 'business', 'units', 'and', 'acrosscontinents', '.this', 'final', 'implementation', 'will', 'retire', 'multiple', ',', 'disparate', 'systems', 'and', 'replacethem', 'with', 'a', 'common', ',', 'integrated', 'system', 'encompassing', 'many', 'processes', 'includingpayroll', ',', 'timekeeping', '...']```

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM