Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - 'str' object has no attribute 'close'

Tags:

python

file-io

I am having a great time trying to figure out why there doesn't need to be a closing attribute for this few lines of code I wrote:

from sys import argv
from os.path import exists

script, from_file, to_file = argv

file_content = open(from_file).read()
new_file = open(to_file, 'w').write(file_content)

new_file.close()

file_content.close()

I read some things and other people's posts about this, but their scripts were a lot more complicated than what I'm currently learning, so I couldn't figure out why.

I am doing Learning Python the Hard Way and would appreciate any help.

like image 269
Re-l Avatar asked Aug 25 '12 02:08

Re-l


People also ask

How to fix str object has no attribute?

The Python "AttributeError: 'str' object has no attribute" occurs when we try to access an attribute that doesn't exist on string objects. To solve the error, make sure the value is of the expected type before accessing the attribute.

How does Python handle attribute errors?

Solution for AttributeError Errors and exceptions in Python can be handled using exception handling i.e. by using try and except in Python. Example: Consider the above class example, we want to do something else rather than printing the traceback Whenever an AttributeError is raised.

What is TypeError str object is not callable?

The result was the TypeError: 'str' object is not callable error. This is happening because we are using a variable name that the compiler already recognizes as something different. To fix this, you can rename the variable to a something that isn't a predefined keyword in Python. Now the code works perfectly.


2 Answers

file_content is a string variable, which contains contents of the file -- it has no relation to the file. The file descriptor you open with open(from_file) will be closed automatically: file sessions are closed after the file-objects exit the scope (in this case, immediately after .read()).

like image 125
Aapo Kyrola Avatar answered Sep 20 '22 08:09

Aapo Kyrola


open(...) returns a reference to a file object, calling read on that reads the file returning a string object, calling write writes to it returning None, neither of which have a close attribute.

>>> help(open)
Help on built-in function open in module __builtin__:

open(...)
    open(name[, mode[, buffering]]) -> file object

    Open a file using the file() type, returns a file object.  This is the
    preferred way to open a file.

>>> a = open('a', 'w')
>>> help(a.read)
read(...)
    read([size]) -> read at most size bytes, returned as a string.

    If the size argument is negative or omitted, read until EOF is reached.
    Notice that when in non-blocking mode, less data than what was requested
    may be returned, even if no size parameter was given.
>>> help(a.write)
Help on built-in function write:

write(...)
    write(str) -> None.  Write string str to file.

    Note that due to buffering, flush() or close() may be needed before
    the file on disk reflects the data written.

Theres a couple ways of remedying this:

>>> file = open(from_file)
>>> content = file.read()
>>> file.close()

or with python >= 2.5

>>> with open(from_file) as f:
...     content = f.read()

The with will make sure the file is closed.

like image 23
Samy Vilar Avatar answered Sep 19 '22 08:09

Samy Vilar