Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What Python module does the file() method belong to? [duplicate]

Tags:

python

I am reading a blog post related to a Recurrent Neural Network for Natural Language Processing and am trying to recreate the code to practice with. The sample code uses a method to read in a .txt file called file().read(). I am not familiar with this method and would like to know if it's contained in an importable module, or at least what it would return so I could recreate the method with with different code.

I did attempt to substitute a with open(filename) as f but it did not return the data in the same format as the file().read() method seems to. "File" isn't the easiest term to Google if you're looking for a specific result!

def train_char_lm(fname, order=4):
    data = file(fname).read()
    lm = defaultdict(Counter)
    pad = "~" * order
    data = pad + data
    for i in xrange(len(data)-order):
        history, char = data[i:i+order], data[i+order]
        lm[history][char]+=1
    def normalize(counter):
        s = float(sum(counter.values()))
        return [(c,cnt/s) for c,cnt in counter.iteritems()]
    outlm = {hist:normalize(chars) for hist, chars in lm.iteritems()}
    return outlm
like image 514
Epiphannie Avatar asked Mar 05 '26 17:03

Epiphannie


1 Answers

Typing

help(file)

into the IDLE interpreter yields

Help on class file in module __builtin__:

plus additional useful info for you. It is part of the built-in module.

like image 145
17th Lvl Botanist Avatar answered Mar 08 '26 08:03

17th Lvl Botanist



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!