Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python file() function

I've been adapting an old piece of code to be Python 3 compliant and I came across this individual script

"""Utility functions for processing images for delivery to Tesseract"""

import os


def image_to_scratch(im, scratch_image_name):
    """Saves image in memory to scratch file.  .bmp format will be read 
        correctly by Tesseract"""
    im.save(scratch_image_name, dpi=(200, 200))


def retrieve_text(scratch_text_name_root):
    inf = file(scratch_text_name_root + '.txt')
    text = inf.read()
    inf.close()
    return text


def perform_cleanup(scratch_image_name, scratch_text_name_root):
    """Clean up temporary files from disk"""
    for name in (scratch_image_name, scratch_text_name_root + '.txt',
                 "tesseract.log"):
        try:
            os.remove(name)
        except OSError:
            pass

On the second function, retrieve_text the first line fails with:

Traceback (most recent call last):
  File ".\anpr.py", line 15, in <module>
    text = image_to_string(Img)
  File "C:\Users\berna\Documents\GitHub\Python-ANPR\pytesser.py", line 35, in image_to_string
    text = util.retrieve_text(scratch_text_name_root)
  File "C:\Users\berna\Documents\GitHub\Python-ANPR\util.py", line 10, in retrieve_text
    inf = file(scratch_text_name_root + '.txt')
NameError: name 'file' is not defined

Is this a deprecated function or another problem alltogether? Should I be replacing file() with something like open()?

like image 363
Bernardo Meurer Avatar asked Aug 21 '15 02:08

Bernardo Meurer


People also ask

What does file write () do?

The write() method writes a specified text to the file. Where the specified text will be inserted depends on the file mode and stream position. "a" : The text will be inserted at the current file stream position, default at the end of the file.

What is file close () in Python?

Python file method close() closes the opened file. A closed file cannot be read or written any more. Any operation, which requires that the file be opened will raise a ValueError after the file has been closed. Calling close() more than once is allowed.

What is the use of tell () method in files?

tell() method can be used to get the position of File Handle. tell() method returns current position of file object. This method takes no parameters and returns an integer value. Initially file pointer points to the beginning of the file(if not opened in append mode).

How does open () work in Python?

Python open() Function The open() function opens a file, and returns it as a file object. Read more about file handling in our chapters about File Handling.


2 Answers

In Python 2, open and file are mostly equivalent. file is the type and open is a function with a slightly friendlier name; both take the same arguments and do the same thing when called, but calling file to create files is discouraged and trying to do type checks with isinstance(thing, open) doesn't work.

In Python 3, the file implementation in the io module is the default, and the file type in the builtin namespace is gone. open still works, and is what you should use.

like image 123
user2357112 supports Monica Avatar answered Oct 04 '22 04:10

user2357112 supports Monica


You can do what the documentation for file() suggests -

When opening a file, it’s preferable to use open() instead of invoking this constructor directly.

You should use open() method instead.

like image 36
Anand S Kumar Avatar answered Oct 04 '22 03:10

Anand S Kumar